Problems with missing collection method in has_many relationship.

S

sean.swolfe

I have a layered one to many relationship situation that is having some
weird problems. I've been trying to look for a solution but I can't
seem to find it, or am not certain what keywords I should search on.

Okay lets explain the environment. A Ruby on Rails app, simple CMS.
Just some little tweaks. But versioning needs to be supported, and some
articles require that the body can have multiple pages. Also the
Articles can have multiple types, so they use a Single table
inheritance scheme.

So I have the following classes.

class Article < ActiveRecord::Base
has_many :versions

def new_version(properties = {})
self.pinned_version += 1
properties[:version] = self.pinned_version
self.versions << Version.new(properties)
end
end

class News < Article
end
# there are other sub classes of articles but for brevity I only
display one for example.

class Version < ActiveRecord::Base
belongs_to :article
has_many :article_pages
end

class ArticlePage < ActiveRecord::Base
belongs_to :version
end

Now in my controller, I have a method that works like this...

# Method to create a new Article. Create's it with the initiated type
# and creates the first version of the Article.
def create
@article = create_typed_article(params[:article])
@version = @article.new_version(params[:version])

params[:article_page].each do |page_name, page_data|
page = ArticlePage.new()
page.page_number = page_name.match(/\d+/)[0]
page.body = page_data
@version.article_pages << page
end

if @article.save
flash[:notice] = 'Article was successfully created.'
redirect_to :action => 'list'
else
render :action => 'new'
end
end

Now the problem is, on the line "@version.article_pages << page", I get
the following error:
undefined method `article_pages' for Version:Class

But if I open up the console, and do the following...
v = Version.find:)conditions => ['version = ? AND article_id = ?', 1,
1])
v.article_pages

it works just fine. Now looking more closely at the error, it appears
that the Version object that is returned by new_version inherits from
Class instead of ActiveRecord::Base. Therefore it doesn't seem to have
the collection of ArticlePage objects.

Well, Ruby doesn't do casting, but how do I get object returned by
collection methods to stay as ActiveRecord::Base classes?

I guess i can change my method to this:
def new_version(properties = {})
self.pinned_version += 1
properties[:version] = self.pinned_version
version = Version.new(properties)
self.versions << version
version
end

But that just seems to circumvent some of the niceities of the Ruby
language, and might as well be programming in Java or C#.

Anyone else run into this problem? Is there a solution? Is this a bug,
or by some sort of wicked design?

Thanks,

Sean
 
E

Eero Saynatkari

I have a layered one to many relationship situation that is having some
weird problems. I've been trying to look for a solution but I can't
seem to find it, or am not certain what keywords I should search on.

Okay lets explain the environment. A Ruby on Rails app, simple CMS.
Just some little tweaks. But versioning needs to be supported, and some
articles require that the body can have multiple pages. Also the
Articles can have multiple types, so they use a Single table
inheritance scheme.

<snip>

Now the problem is, on the line "@version.article_pages << page", I get
the following error:
undefined method `article_pages' for Version:Class

<snip>

Anyone else run into this problem? Is there a solution? Is this a bug,
or by some sort of wicked design?

The 'Version:Class' is your best clue: it means that instead of
Version.article_pages you are calling Version.new.article_pages.
From this we can extrapolate that you are for some reason assigning
the class (or retrieving the class) rather than an instance thereof.

In any case, you may have better luck on the Rails mailing list:

http://lists.rubyonrails.org/mailman/listinfo/rails

Thanks,

Sean


E
 
E

Eero Saynatkari

I have a layered one to many relationship situation that is having some
weird problems. I've been trying to look for a solution but I can't
seem to find it, or am not certain what keywords I should search on.

Okay lets explain the environment. A Ruby on Rails app, simple CMS.
Just some little tweaks. But versioning needs to be supported, and some
articles require that the body can have multiple pages. Also the
Articles can have multiple types, so they use a Single table
inheritance scheme.

<snip>

Now the problem is, on the line "@version.article_pages << page", I get
the following error:
undefined method `article_pages' for Version:Class

<snip>

Anyone else run into this problem? Is there a solution? Is this a bug,
or by some sort of wicked design?

The 'Version:Class' is your best clue: it means that instead of
Version.new.article_pages you are calling Version.article_pages.
From this we can extrapolate that you are for some reason assigning
the class (or retrieving the class) rather than an instance thereof.


In any case, you may have better luck on the Rails mailing list:

http://lists.rubyonrails.org/mailman/listinfo/rails

Thanks,

Sean


E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top