ruby - Adding a scope to a has_many through association in Rails -


i have project , user models joined membership model. want retrieve project's members except 1 user.

project.members.where.not(id: current_user) 

like answer, want use scope:

class user < activerecord::base   scope :except, ->(user) { where.not(id: user) } end 

but doesn't work.

p.members.except(user.find(1)) user load (1.0ms)   select "users".* "users"   "users"."id" = $1 limit 1  [["id", 1]]  user load (0.4ms)   select "users".* "users"   inner join "memberships" on "users"."id" = "memberships"."user_id"   "memberships"."project_id" = $1  [["project_id", 2]] 

as can see results in 2 queries, not one. , returns members, not taking account exclusion.

why doesn't work?

try renaming scope else, except_for or all_except. name except used active_record

http://api.rubyonrails.org/classes/activerecord/spawnmethods.html#method-i-except

also 2 queries because doing user.find(1) results in first query.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -