# Has many :through a named scope

**URL:** <https://www.ruby-forum.com/t/has-many-through-a-named-scope/160863>\
**Category:** Rails\
**Created:** [February 20, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/has-many-through-a-named-scope/160863 "2009-02-20T13:39:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [February 20, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/has-many-through-a-named-scope/160863/1 "2009-02-20T13:39:17Z")

</div>

class User

has\_many :friendships  
has\_many :friends, :through =\> :friendship  
end

class Friendship  
belongs\_to :user  
belongs\_to :friend, :class\_name =\> ‘User’  
named\_scope :accepted, :conditons =\> “is\_accepted = true”  
named\_scope :pending, :conditons =\> “is\_accepted = false”  
end

Here in the User class, i want to write  
has\_many :pending\_friends, through a named scope friendships accepted  
has\_many:accepted\_friends through a named scope pending

Please help,  
Regards,  
Pankaj

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [February 20, 2009, 6:09pm UTC](https://www.ruby-forum.com/t/has-many-through-a-named-scope/160863/2 "2009-02-20T18:09:55Z")

</div>

I’m also trying to find a way of doing this if anybody has any  
suggestions; will report back if I find a way of doing it.

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [February 20, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/has-many-through-a-named-scope/160863/3 "2009-02-20T18:24:37Z")

</div>

Hi Pankaj,

I don’t know how to do this with named\_scope, but maybe you’ll like  
this:

class User  
has\_many :pending\_friends, :through =\> :friendship, :source =\>  
:friend, :conditions =\> {‘friendships.is\_accepted’ =\> ‘true’}  
end

However, you can’t do sth. like

@user.pending\_friends \<\< @friend

anymore in this way. See:

[http://rails.lighthouseapp.com/projects/8994/tickets/718-has\_many-method-doesn-t-handle-conditions](http://rails.lighthouseapp.com/projects/8994/tickets/718-has_many-method-doesn-t-handle-conditions)

Jan

- pankaj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2009-02-20 04:30:09 -0800]:

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [February 20, 2009, 6:49pm UTC](https://www.ruby-forum.com/t/has-many-through-a-named-scope/160863/4 "2009-02-20T18:49:28Z")

</div>

Using :conditions will work but the problem is that you’ve just  
introduced duplication between the conditions and the named\_scope.  
Anyway, here’s my initial pass at trying to solve this:

[https://gist.github.com/82c50486bc38b7d7c880](https://gist.github.com/82c50486bc38b7d7c880)

It only works for conditions at the moment which was enough for me;  
please do fork the gist and hack away!

Cheers  
Luke
