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.findconditions => ['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
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.findconditions => ['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