I posted this originally on the comp.lang.ruby newsgroup, but I was told
to
bring it here.
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, it seems that the problem is that the @version.article_pages <<
method
returns an object with the wrong base type. Other methods from the
collection such as find and the index return an object with the right
base
type.
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 W.
master nerd of
i heart squares, Co.
3711 N. Ravenswood Ave. #147 Chicago, IL 60613
Ph. (773) 531-6301 Fx. (773) 529-7041
http://www.iheartsquares.com