开发者

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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜