Eric Hahn
2009-Oct-05 20:57 UTC
doing creates on has_many_through associations with :conditions?
I''m confused about something I thought should work. I have tables Users
and Blogs, joined together through Memberships. Membership also has a boolean
for moderator-ness. So, the Blog model looks like this:
class Blog < ActiveRecord::Base
has_many :memberships
has_many :users,
:through => :memberships
has_many :moderators,
:through => :memberships,
:source => :user,
:conditions => { :memberships => {:moderator => true} }
end
Imagine something trivial like this:
b = Blog.create(:name => ''politics'')
user1 = User.create(:name => ''billg'')
user2 = User.create(:name => ''dhh'')
b.users << user1
b.moderators << user1
b.save!
I see this
b.memberships
+----+---------+---------+-----------+
| id | user_id | blog_id | moderator |
| 1 | 1 | 1 | |
| 2 | 2 | 1 | |
+----+---------+---------+-----------+
Which seems plain wrong. I''d think the moderator column should be
''t'' for the second user''s membership since he was
added via the :moderators association which has a conditions clause.
What am I misunderstanding? Thanks much.
-Eric
ps: Rails 2.3.4
pps: no apparent difference if I rewrite the conditions to array-style.
ppps: it has been mentioned that ticket #2998
https://rails.lighthouseapp.com/projects/8994/tickets/2998 is related, but that
seems to be about the query side, not the record-creation side.