Model: self is not child's parent

Seems to work perfectly for me.

parent.rb class Parent < ActiveRecord::Base   has_many :children end

child.rb class Child < ActiveRecord::Base   belongs_to :parent end

parent_test.rb   def test_child_parent_is_parent     parent = Parent.find(:first)     assert_equal parent, parent.children[0].parent   end

003_load_test_data.rb class LoadTestData < ActiveRecord::Migration   def self.up     parent = Parent.create(:name => "Robert")     Child.create(:name => "Bill", :parent => parent)     Child.create(:name => "Sally", :parent => parent)   end

  def self.down     Child.delete_all     Parent.delete_all   end end

Test results: Rigel$ ruby test/unit/parent_test.rb Loaded suite test/unit/parent_test Started . Finished in 0.10244 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

But you are correct that this is two separate instances of the Parent object. Rails does not seem to have a uniquing system so you'll need to compare object equality using methods supplied by ActiveRecord, which is how "assert_equal" is going to determine equality.

Basically, you have two separate instances of Parent representing the same row in the parents table. Building a system that ensures object equality in object relational mapping is a pretty complex endeavor. It appears that Rails makes no attempt to do this.

A bit more detail too offer.....