1
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?

2
  • Is that really || and && and not OR and AND? Commented Aug 6, 2010 at 19:00
  • When I learned SQL, I used the symbols instead of the words. =\ but I guess MySQL can do both. Commented Aug 6, 2010 at 20:21

1 Answer 1

1

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(',')] }
     }
Sign up to request clarification or add additional context in comments.

5 Comments

I think you'd need to collect the id's for that to work: users.collect(&:id).join(',')
@mark Yes you'd be correct if the argument was an array of User objects but I understood from the question that it would be an array of IDs. Either way, I agree, the caller must do the collect either before or inside the named scope
You're right, if it were to have worked for a single user then it would also have needed an id. Naming convention is to blame. :)
shhh. naming some stuff is hard. (not here, but like named scope with two parameters where the visibility can change and so can the user_ids object) =p
actually this isn't what I want. =( posting more specific question soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.