has many :through a named scope

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

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.

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

Jan

* pankaj <pankajbhageria@gmail.com> [2009-02-20 04:30:09 -0800]:

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

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

Cheers Luke