# Problems with missing collection method in has\_many relation

**URL:** <https://www.ruby-forum.com/t/problems-with-missing-collection-method-in-has-many-relation/97197>\
**Category:** Ruby\
**Created:** [December 12, 2005, 3:24am UTC](https://www.ruby-forum.com/t/problems-with-missing-collection-method-in-has-many-relation/97197 "2005-12-12T03:24:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 12, 2005, 3:24am UTC](https://www.ruby-forum.com/t/problems-with-missing-collection-method-in-has-many-relation/97197/1 "2005-12-12T03:24:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 12, 2005, 8:43pm UTC](https://www.ruby-forum.com/t/problems-with-missing-collection-method-in-has-many-relation/97197/2 "2005-12-12T20:43:41Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 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.
> 
> Now the problem is, on the line “@version.article\_pages \<\< page”, I get  
> the following error:  
> undefined method `article\_pages’ for Version:Class
> 
> 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](http://lists.rubyonrails.org/mailman/listinfo/rails)

> Thanks,
> 
> Sean

E
