Activerecord failed?


#1

Hello all,
i found something wrong (or maybe i was wrong?)
this looks like a rectiverecord bug , can anyone help me to point
out what’s going on?

------------------migration
create_table :books do |t|
t.column :name,:string
t.column :person_id,:integer
end

create_table :people do |t|
  t.column :name ,:string
end

------------------model
class Book < ActiveRecord::Base
belongs_to :person
end

class Person < ActiveRecord::Base
has_many :books
end

------------------in console , try the following:

book=Book.create(:name=>‘book1’)
kevin=Person.create(:name=>‘kevin’)
kevin.books # should be empty []

book.person=kevin
book.save

kevin.books # why still nothing ???

Person.find(kevin.id).books # have books!


#2

because you already loaded the association when you did kevin.books
the first time.

book = Book.create(…)
kevin - Person.create(…)
kevin.books # empty, now association is loaded, it won’t be reloaded
unless you force it

book.person = kevin
book.save

kevin.books(true) # passing true forces reload, should show associated
books


#3

book=Book.create(:name=>‘book1’)
kevin=Person.create(:name=>‘kevin’)
kevin.books # should be empty []

book.person=kevin
book.save

kevin.books # why still nothing ???

Person.find(kevin.id).books # have books!

kevin.reload