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

前端 未结 7 1017
时光说笑
时光说笑 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

    0 讨论(0)
  • 2020-12-28 14:00

    WRONG This is what I used to do for some reason.

    keys = params[:search].split(',').map!(&:downcase)
    # keys are now ['brooklyn', 'queens']
    
    query = 'lower(city) LIKE ?'
    
    if keys.size > 1
      # I need something like this depending on number of keys 
      # 'lower(city) LIKE ? OR lower(city) LIKE ? OR lower(city) LIKE ?'
     query_array = []
     keys.size.times { query_array << query }
     #['lower(city) LIKE ?','lower(city) LIKE ?']
     query = query_array.join(' OR ')
     # which gives me 'lower(city) LIKE ? OR lower(city) LIKE ?'
    end
    # now I can query my model
    # if keys size is one then keys are just 'brooklyn', 
    # in this case it is  'brooklyn', 'queens'
    # @posts = Post.where('lower(city) LIKE ? OR lower(city) LIKE ?','brooklyn', 'queens' )
    @posts = Post.where(query, *keys )
    

    now however - yes - it's very simple. as nfriend21 mentioned

    Model.where(attribute: [value1,value2])

    does the same thing

    0 讨论(0)
  • 2020-12-28 14:01

    You can use a hash rather than a string. Build up a hash with however many conditions and corresponding values you are going to have and put it into the first argument of the where method.

    0 讨论(0)
  • 2020-12-28 14:02

    This is actually pretty simple:

    Model.where(attribute: [value1,value2])
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-28 14:05

    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.

    0 讨论(0)
提交回复
热议问题