Hi there, I would like to find more information about query caching, or about the concept behind the following behavior, in a "User has many Groups through Memberships" association. Let me show you an example. # Base : user = User.find_by_email(''john.doe-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org'') group = List.create( :title => "First group" ) group2 = List.create( :title => "Second group" ) # if I do this : Membership.create(:group => group, :user => user) Membership.create(:group => group2, :user => user) user.groups.each do |g| puts g.title end # the result is as expected First group Second group # But if do this : Membership.create(:group => group, :user => user) user.groups.each do |g| puts g.title end Membership.create(:group => group2, :user => user) user.groups.each do |g| puts g.title end # the result is : First group First group # instead of First group First group Second group What''s the magic behind that ? Also, what''s the best way to create new memberships and avoid troubles ? Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
On 4 Mar 2008, at 11:28, aJo aJo wrote:> > Hi there, > > I would like to find more information about query caching, or about > the > concept behind the following behavior, in a "User has many Groups > through Memberships" association. > Let me show you an example. > > # Base : > > user = User.find_by_email(''john.doe-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org'') > group = List.create( :title => "First group" ) > group2 = List.create( :title => "Second group" ) > > # if I do this : > > Membership.create(:group => group, :user => user) > Membership.create(:group => group2, :user => user) > > user.groups.each do |g| puts g.title end > > # the result is as expected > First group > Second group > > # But if do this : > > Membership.create(:group => group, :user => user) > user.groups.each do |g| puts g.title end > Membership.create(:group => group2, :user => user) > user.groups.each do |g| puts g.title end >This isn''t a query caching problem, it''s activerecord''s association caching (so if you were to add a user.reload or a user.groups.reload in there you would get the right answer) Fred> # the result is : > First group > First group > > # instead of > First group > First group > Second group > > What''s the magic behind that ? > Also, what''s the best way to create new memberships and avoid > troubles ? > > Thanks > -- > 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 -~----------~----~----~----~------~----~------~--~---
OK, I indeed found that a .reload could do the trick. But I suppose we are not condemned to do a .reload before every call to an association, so how do you avoid that ? -- 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 -~----------~----~----~----~------~----~------~--~---