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

前端 未结 7 1065
时光说笑
时光说笑 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 14:02

    Instead of passing the same parameter multiple times to where() like this

    User.where(
      "first_name like ? or last_name like ? or city like ?", 
      "%#{search}%", "%#{search}%", "%#{search}%"
    )
    

    you can easily provide a hash

    User.where(
      "first_name like :search or last_name like :search or city like :search",
      {search: "%#{search}%"}
    )
    

    that makes your query much more readable for long argument lists.

提交回复
热议问题