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
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 Dodwell