How to specify multiple values in where with AR query interface in rails3

前端 未结 7 1081
时光说笑
时光说笑 2020-12-28 13:31

Per section 2.2 of rails guide on Active Record query interface here:

which seems to indicate that I can pass a string specifying the condition(s), then an array of

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 13:59

    Sounds like you're doing something like this:

    Model.where("attribute = ? OR attribute2 = ?", [value, value])
    

    Whereas you need to do this:

    #notice the lack of an array as the last argument
    Model.where("attribute = ? OR attribute2 = ?", value, value) Have a
    

    look at http://guides.rubyonrails.org/active_record_querying.html#array-conditions for more details on how this works.

    Was really close. You can turn an array into a list of arguments with *my_list.

    Model.where("id = ? OR id = ?", *["1", "2"])
    

    OR

    params = ["1", "2"]
    Model.where("id = ? OR id = ?", *params)
    

    Should work

提交回复
热议问题