Hi all, I am having problems with some reasonably complex assocaitions
involving has_many and belongs_to with the :through option.
I have three models:
class Site < ActiveRecord::Base
has_many :site_page_relationships
has_many :pages, :through =>
:pagesite_page_relationships_relationships, :source => :child_page
end
class PageRelationship < ActiveRecord::Base
belongs_to :site
belongs_to :parent_page, :foreign_key => :parent_page_id,
:class_name => :page
belongs_to :child_page, :foreign_key => :child_page_id,
:class_name => :page
end
class Page < ActiveRecord::Base
has_many :page_relationships, :foreign_key => :child_page_id
has_many :child_pages, :through => :page_relationships,
:source => :child_page
has_many :parent_pages, :through => :page_relationships,
:source => :parent_page
end
The idea being that you can have multipe sites each with their own
sitetree, and the ability to share pages between sites but in different
positions in the site tree.
When I try the following in ‘script/console’:
page1 = Page.new
=> #<Page:0x279bb60 @attributes={}, @new_record=true>
page1.save
=> true
page2 = Page.new
=> #<Page:0x27944b4 @attributes={}, @new_record=true>
page2.save
=> true
site = Site.new
=> #<Site:0x278e208 @attributes={“name”=>nil, “updated_at”=>nil,
“url”=>nil, “description”=>nil, “created_at”=>nil}, @new_record=true>
site.save
=> true
rel1 = PageRelationship.new(:parent_page => page1, :child_page => page2, :site => site)
TypeError: can’t convert Symbol into String
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1003:in
type_name_with_module' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1242:in
compute_type’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/reflection.rb:112:in
klass' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/association_proxy.rb:133:in
raise_on_type_mismatch’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/belongs_to_association.rb:22:in
replace' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations.rb:850:in
child_page=’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1510:in
attributes=' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1509:in
attributes=’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1354:in
initialize_without_callbacks' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:236:in
initialize’
from (irb):11
from :0
You can see that I get this error? It’s really bugging me, I can’t
figure out why. Any suggestions/ideas would be much appreciated.
Many Thanks
Mark D.
:class_name => :page
end
The idea being that you can have multipe sites each with their own
sitetree, and the ability to share pages between sites but in different
positions in the site tree.
If I understood your domain, it is like :
Site 1 -----> SiteTree 1
Site 2 ------> SiteTree 2
and so on.
Then SiteTree has many Pages. So it looks like you have one-to-one
relationship between Site and SiteTree. SiteTree and Pages many to many
relationship.
If that is the case then your model should break the many-to-many
relationship between SiteTree and Page classes.
[email protected] wrote:
Hi all, I am having problems with some reasonably complex assocaitions
involving has_many and belongs_to with the :through option.
I have three models:
class Site < ActiveRecord::Base
has_many :site_page_relationships
has_many :pages, :through =>
:pagesite_page_relationships_relationships, :source => :child_page
end
class PageRelationship < ActiveRecord::Base
belongs_to :site
belongs_to :parent_page, :foreign_key => :parent_page_id,
:class_name => :page
belongs_to :child_page, :foreign_key => :child_page_id,
:class_name => :page
end
class Page < ActiveRecord::Base
has_many :page_relationships, :foreign_key => :child_page_id
has_many :child_pages, :through => :page_relationships,
:source => :child_page
has_many :parent_pages, :through => :page_relationships,
:source => :parent_page
end
The idea being that you can have multipe sites each with their own
sitetree, and the ability to share pages between sites but in different
positions in the site tree.
When I try the following in ‘script/console’:
page1 = Page.new
=> #<Page:0x279bb60 @attributes={}, @new_record=true>
page1.save
=> true
page2 = Page.new
=> #<Page:0x27944b4 @attributes={}, @new_record=true>
page2.save
=> true
site = Site.new
=> #<Site:0x278e208 @attributes={“name”=>nil, “updated_at”=>nil,
“url”=>nil, “description”=>nil, “created_at”=>nil}, @new_record=true>
site.save
=> true
rel1 = PageRelationship.new(:parent_page => page1, :child_page => page2, :site => site)
TypeError: can’t convert Symbol into String
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1003:in
`type_name_with_module’
Your Page model doesn’t have the right associations for a
self-referential hmt relationship. You need two different has_many
assocs for the parent and child refs to the join model. This is like
having different aliases for your joins in SQL. Take a look at my
posting on this:
http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through
Also, your Site class has some weird named assocs. That might be the
source of your error. Remember, if the model name can’t be inferred from
the assoc name, you need to specify it with options.
–
Josh S.
http://blog.hasmanythrough.com
Bala P. wrote:
Also, your Site class has some weird named assocs. That might be the
source of your error. Remember, if the model name can’t be inferred from
the assoc name, you need to specify it with options.
Edge (n) <------------------->(1) Node -----
^_____|
Is the diagram correct? Node has the self-reference and you have one to
many
relationship between the Node and the Edges.
Yes.
–
Josh S.
http://blog.hasmanythrough.com
Also, your Site class has some weird named assocs. That might be the
source of your error. Remember, if the model name can’t be inferred from
the assoc name, you need to specify it with options.
Edge (n) <------------------->(1) Node -----
^_____|
Is the diagram correct? Node has the self-reference and you have one to
many
relationship between the Node and the Edges.
Many thanks for all your help, I have now managed to fix it. After
reading John’s post ’ Self-referential has_many :through associations’
I sorted it.
Great to get such a quick response from people. First time I posted a
question here! Was very happy indeed. My new models which work are as
follows:
class Site < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :page_relationships
has_many :pages, :through => :page_relationships, :source =>
:child_page
end
class PageRelationship < ActiveRecord::Base
belongs_to :site
belongs_to :parent_page, :foreign_key => ‘parent_page_id’,
:class_name => ‘Page’
belongs_to :child_page, :foreign_key => ‘child_page_id’,
:class_name => ‘Page’
end
class Page < ActiveRecord::Base
has_many :page_relationships_as_child, :foreign_key =>
‘child_page_id’,
:class_name => ‘PageRelationship’
has_many :page_relationships_as_parent, :foreign_key =>
‘parent_page_id’,
:class_name => ‘PageRelationship’
has_many :child_pages, :through => :page_relationships_as_child
has_many :parent_pages, :through => :page_relationships_as_parent
end
Cheers again, Mark