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!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 On 3/27/07, Kevin Yang <intelligent113-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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! > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---