ruby - How to magically supply Active Record scopes with arguments? -


i'm not sure possible, let's see if 1 of comes solution. more or less code quality in terms of readability , not actual problem because have solution. have friendship model , user model. friendship model used model friendships between 2 users:

class friendship    def self.requested(user)     where(:user_id => user).where(:status => 'requested')   end    def self.pending(user)     where(:user_id => user).where(:status => 'pending')   end    def self.accepted(user)     where(:user_id => user).where(:status => 'accepted')   end    # ...  end  class user    has_many :friendships     # ... end 

is somehow possible call requested, pending or accepted scope through user model without providing argument?

a_user.friendships.pending # not work, there way working?  a_user.friendships.pending(a_user) # works of course! 

i think should work if take argument off. calling pending off of user object should scope friendships appropriate user. define method this:

def self.pending   where(:status => 'pending') end 

and call:

a_user.friendships.pending 

check logs generated query if you're not sure it's working.

if still want call passing argument i'd name method friendship.pending_for(user).


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -