0

Both privacy and users are arrays. I've taken out some logic to show the important stuff.

  named_scope :non_employees_and_beloning_to_users, lambda { |args|
    privacy = args[0]
    users = args[1]
    { :conditions =>  ["(visibility = ? && user_id = ?)", privacy, users] }
  }

but basically, the above generates the below, and when typed into a MySQL editor thing, no results show up. so, I came to believe this isn't the correct method for searching for stuff in mySOL with an array?

SELECT * 
FROM `proposals`  
WHERE visibility = '1,0' && user_id = '-1,8,9,11';

I want it to have the effect of

visibility = (1 or 0 ) AND user_id = (-1 or 8 or 9 or 11)
2
  • Arrays of id's or of objects? :) Commented Aug 6, 2010 at 20:45
  • of ids. objects are too big =) Commented Aug 6, 2010 at 22:40

2 Answers 2

1

Since it seems that you have a comma separated list of values in visibility and users you could use MySQL's IN() function. Something like:

visibility IN (1,0) AND user_id IN (-1,8,9,11)

(see here for more info on IN(): http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in)

Alternatively you could convert visibility and users to Ruby arrays and then generate similar SQL code manually.

Sign up to request clarification or add additional context in comments.

1 Comment

They are ruby arrays. how do I do that?
0

If args[0] and args[1] are arrays of ids, you just do this:

named_scope :non_employees_and_belonging_to_users, lambda { |args|
  privacy = args[0]
  users = args[1]
  { :conditions =>  ["visibility IN (?) AND user_id IN (?)", privacy, users] }
}

You need to wrap your ? placeholders in parens for SQL's IN to work. This should generate:

SELECT * FROM `proposals` WHERE visibility IN (1,0) AND user_id IN (-1,8,9,11);

Comments

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.