Forum: Ruby Problems with missing collection method in has_many relation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
499f4dccdedd68c1f51ee5a929a0c027?d=identicon&s=25 sean.swolfe@gmail.com (Guest)
on 2005-12-12 03:24
(Received via mailing list)
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
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2005-12-12 20:43
sean.swolfe@gmail.com 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.
>
> <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
This topic is locked and can not be replied to.