# Doing creates on has\_many\_through associations with :conditions?

**URL:** <https://www.ruby-forum.com/t/doing-creates-on-has-many-through-associations-with-conditions/176761>\
**Category:** Rails\
**Created:** [October 5, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/doing-creates-on-has-many-through-associations-with-conditions/176761 "2009-10-05T23:39:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [October 5, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/doing-creates-on-has-many-through-associations-with-conditions/176761/1 "2009-10-05T23:39:45Z")

</div>

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  
[#2998 [PATCH] SQL generated by find ignores :include option - Ruby on Rails - rails](https://rails.lighthouseapp.com/projects/8994/tickets/2998) is related,  
but that seems to be about the query side, not the record-creation side.
