Hello,
I have a User ActiveRecord model and a Group AR model joined by has_many
:through Membership. It''s not a HBTM because I need an extra attribute
for the memberships.
I tried to fill my dev site with some data added via a migration file. I
want to create two groups, and add a user to them. Below is the code I
use, and a description of the behaviour I don''t understand.
Code : - fold - unfold
1. user =
User.find_by_email(''bob-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org'')
2.
3. group = Group.new
4. group.title = "First group"
5. group.save(false)
6.
7. membership = Membership.new
8. membership.group = group
9. membership.user = user
10. membership.credit = 10
11. membership.save(false)
12.
13. puts "membership 1"
14. user.groups.each do |g| puts g.title end
15.
16. # No problem here, output is :
17. # membership 1
18. # First group
19.
20. group2 = Group.new
21. group2.title = "Second group"
22. group2.save(false)
23.
24. membership2 = Membership.new
25. membership2.group = group2
26. membership2.user = user
27. membership2.credit = 15
28. membership2.save(false)
29.
30. puts "membership 2"
31. user.lists.each do |g| puts g.title end
32.
33. # Output is only :
34. # membership 2
35. # First group
36.
37. user2 =
User.find_by_email(''bob-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org'')
38.
39. puts "memberships"
40. user2.groups.each do |g| puts g.title end
41.
42. # Output is :
43. # memberships
44. # First group
45. # Second group
Why was my first group dynamically added to the user''s groups, and not
the second one ?
Thank you for your attention
--
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
-~----------~----~----~----~------~----~------~--~---