Rails help finder method
I am trying to create a find method. That should find all konkurrencer that have the tid "1 min" or "2 min" and the form is "Nyhedsbrev".
I have tried something like this:
Konkurrancer.where("tid = ? or ? and form = ?", "2 min", "1 min", "Nyhedsbrev")
It does开发者_StackOverflow中文版n't work. It only finds all konkurrencer where the form is Nyhedsbrev.
Perhaps this works as intended.
Konkurrancer.where("tid IN (?) AND form = ?", ['2 min', '1 min'], 'Nyhedsbrev')
Other than that, the 'OR' syntac you are trying to use is written like this in SQL:
SELECT * FROM foo WHERE column1 = 'foo' OR column1 = 'BAR'
Notice that for the 'OR' condition you still need to list the column name again.
Your SQL is a bit wrong, I think you want this:
Konkurrancer.where(
"(tid = ? or tid = ?) and form = ?",
"2 min",
"1 min",
"Nyhedsbrev"
)
Note the second mention of the tid
column name and the parentheses.
Try this:
Konkurrancer.where("tid = ? or tid = ?", "2 min", "1 min").where(:form => "Nyhedsbrev")
You can also use:
Konkurrancer.where("form = ? and (tid = ? or tid = ?)", "Nyhedsbrev", "2 min", "1 min")
精彩评论