# Has\_many\_through with conditions on another table (:joins)

**URL:** <https://www.ruby-forum.com/t/has-many-through-with-conditions-on-another-table-joins/150298>\
**Category:** Rails\
**Created:** [October 7, 2008, 2:12am UTC](https://www.ruby-forum.com/t/has-many-through-with-conditions-on-another-table-joins/150298 "2008-10-07T02:12:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 7, 2008, 2:12am UTC](https://www.ruby-forum.com/t/has-many-through-with-conditions-on-another-table-joins/150298/1 "2008-10-07T02:12:34Z")

</div>

Hi,

I have an class that looks like this:

class Section \< ActiveRecord::Base  
belongs\_to :offering, :foreign\_key =\> :parent\_id  
has\_many :memberships, :as =\> :member\_of  
has\_many :students, :through =\> :memberships, :source =\> :user,  
:joins =\> “inner join roles onmemberships.role\_id =  
roles.id”, :conditions =\>“roles.name = ‘Student’”  
end

This doesn’t work, though, because :joins isn’t an allowed key. From  
associations.rb:

```
    def create_has_and_belongs_to_many_reflection(association_id,

```

options, &extension)  
options.assert\_valid\_keys(  
:class\_name, :table\_name, :join\_table, :foreign\_key,  
:association\_foreign\_key,  
:select, :conditions, :include, :order, :group, :limit,  
:offset,  
:uniq,  
:finder\_sql, :delete\_sql, :insert\_sql,  
:before\_add, :after\_add, :before\_remove, :after\_remove,  
:extend, :readonly,  
:validate  
)

I added :joins to the list of allowed keys, and it made my tests pass,  
but I want to check that I’m not doing something silly. Is there a  
particular reason that :joins isn’t an allowed key for  
has\_many :through?

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 7, 2008, 2:20am UTC](https://www.ruby-forum.com/t/has-many-through-with-conditions-on-another-table-joins/150298/2 "2008-10-07T02:20:46Z")

</div>

Correction: I made the change to :create\_has\_many\_reflection

If there is no reason not to allow the :join keyword, what is the best  
way to make the change? I’d prefer not to freeze Rails, but I’m  
having a lot of trouble targeting just this method with a plugin.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 7, 2008, 4:40am UTC](https://www.ruby-forum.com/t/has-many-through-with-conditions-on-another-table-joins/150298/3 "2008-10-07T04:40:04Z")

</div>

I was able to get this to work by using :include =\> {:memberships  
=\> :role} but that’s far from ideal as Ruby will build a lot of  
objects I don’t need, and waste a significant amount of time and  
memory doing it.
