class Invitation < ActiveRecord::Base acts_as_versioned belongs_to :initiator, :class_name => "User", :foreign_key => :initiator_id belongs_to :respondent, :class_name => "User", :foreign_key => :respondent_id belongs_to :waiting_on, :class_name => "User", :foreign_key => :waiting_on end in the unit test: def skip_test_versioned invitation = Invitation.new invitation.initiator = @initiator invitation.respondent = @respondent invitation.waiting_on = @initiator invitation.status = "open" assert invitation.save invitation.reload assert_equal 1, invitation.versions.size invitation.status = "closed" assert invitation.save invitation.reload assert_equal 2, invitation.versions.size invitation.revert_to(invitation.versions.size - 1) # <=== error here assert_equal "open", invitation.status end The error I get is: ActiveRecord::AssociationTypeMismatch: User expected, got Fixnum /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/associations/association_proxy.rb:93:in `raise_on_type_mismatch'' /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/associations/belongs_to_association.rb:25:in `replace'' /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/associations.rb:628:in `waiting_on='' /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/associations.rb:621:in `waiting_on='' /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:242:in `send'' /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:242:in `clone_versioned_model'' /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:241:in `each'' /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:241:in `clone_versioned_model'' /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:204:in `revert_to'' test/unit/invitation_test.rb:34:in `test_versioned'' What''s happening is invitation.versions[1].waiting_on is "5", which is @initiator.id. What I suspect is that the revert_to isn''t doing it''s User.find on the new version''s field because the foreign_key isn''t the standard format. What I haven''t done (because it''s very late), is change :waiting_on to just :waiting and the :foreign_key to :waiting_id to see if it fixes my problem. My acts_as_versioned is 0.1.3. -- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
On 10/6/05, Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote:> class Invitation < ActiveRecord::Base > acts_as_versioned > belongs_to :initiator, :class_name => "User", :foreign_key => :initiator_id > belongs_to :respondent, :class_name => "User", :foreign_key => :respondent_id > belongs_to :waiting_on, :class_name => "User", :foreign_key => :waiting_on > end > > in the unit test: > > def skip_test_versioned > invitation = Invitation.new > invitation.initiator = @initiator > invitation.respondent = @respondent > invitation.waiting_on = @initiator > invitation.status = "open" > assert invitation.save > invitation.reload > assert_equal 1, invitation.versions.size > invitation.status = "closed" > assert invitation.save > invitation.reload > assert_equal 2, invitation.versions.size > invitation.revert_to(invitation.versions.size - 1) # <=== error here > assert_equal "open", invitation.status > end > > The error I get is: > > ActiveRecord::AssociationTypeMismatch: User expected, got Fixnum > /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/associations/association_proxy.rb:93:in `raise_on_type_mismatch'' > /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/associations/belongs_to_association.rb:25:in `replace'' > /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/associations.rb:628:in `waiting_on='' > /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/associations.rb:621:in `waiting_on='' > /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:242:in `send'' > /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:242:in `clone_versioned_model'' > /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:241:in `each'' > /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:241:in `clone_versioned_model'' > /opt/local/lib/ruby/gems/1.8/gems/acts_as_versioned-0.1.3/lib/acts_as_versioned.rb:204:in `revert_to'' > test/unit/invitation_test.rb:34:in `test_versioned'' > > What''s happening is invitation.versions[1].waiting_on is "5", which is > @initiator.id. What I suspect is that the revert_to isn''t doing it''s > User.find on the new version''s field because the foreign_key isn''t the > standard format. > > What I haven''t done (because it''s very late), is change :waiting_on to > just :waiting and the :foreign_key to :waiting_id to see if it fixes > my problem. > > My acts_as_versioned is 0.1.3. > -- > Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.orgI think it''s throwing a fit because your foreign key is the same as the belongs_to. aav is only saving the current table. It loops through the attributes keys and sets the values... record.initiator_id = 1 record.respondent_id = 2 record.waiting_on = 3 I suppose I could bypass that and just call write_attribute? But yours seems like an odd case. I''d rather catch any attribute setters that someone has by calling attr_key= if I can. -- rick http://techno-weenie.net
Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> On 10/6/05, Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote: >> class Invitation < ActiveRecord::Base >> acts_as_versioned >> belongs_to :initiator, :class_name => "User", :foreign_key => :initiator_id >> belongs_to :respondent, :class_name => "User", :foreign_key => :respondent_id >> belongs_to :waiting_on, :class_name => "User", :foreign_key => :waiting_on >> end >> > > I think it''s throwing a fit because your foreign key is the same as > the belongs_to. aav is only saving the current table.I don''t think the saving part is the problem. I think it''s the instantiating the new object in revert_to. I don''t understand why aav cares that my FK is the same as my attribute name. The two should be completely independent. Anyway, what do you suggest for me to fix this? Rename my FK to something else like :waiting_id? -- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
> I don''t think the saving part is the problem. I think it''s the > instantiating the new object in revert_to. I don''t understand why aav > cares that my FK is the same as my attribute name. The two should be > completely independent. > > Anyway, what do you suggest for me to fix this? Rename my FK to > something else like :waiting_id? > -- > Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.orgYes. Normally with associations you have blah.assoc and blah.assoc_id to set the ID. I don''t want to instantiate new association models on rollbacks, I''d rather just set the id and be done with it. This would also be an issue with any forms you have. Normally when you save a model in a controller, you have params like so: { :blah => { :name => ''teddy'', :assoc_id => 5 } } So you save it like: Blah.new(params[:blach]) In your case, you''d have to instantiate the object beforehand just to save it. params[:blah][:assoc_id] = Assoc.find_by_id(params[:blah][:assoc_id]) Blah.new(params[:blah]) -- rick http://techno-weenie.net