NullVoxPopuli
NullVoxPopuli

Reputation: 65173

Ruby on Rails: named scope with lambda and an array

named_scope :all_public, lambda { |users| 
       { :conditions =>  ["visibility = ? || (visibility = ? && user_id = ?)", Shared::PUBLIC, Shared::PRIVATE, users] }
     }

That works nice for one user, but is there a way to modify it to work where users is an array of user ids?

Upvotes: 1

Views: 2460

Answers (1)

bjg
bjg

Reputation: 7477

Something like this and then just pass a single element array for the single ID case

named_scope :all_public, lambda { |users| 
       { :conditions =>  ["visibility = ? OR (visibility = ? AND user_id IN (?))", Shared::PUBLIC, Shared::PRIVATE, users.join(',')] }
     }

Upvotes: 1

Related Questions