Rails where condition with nil value

前端 未结 5 915
我在风中等你
我在风中等你 2021-02-05 03:16

I found out that using where with symbol :my_id => nil and using old school one with ? is different. Could anyone explain me why?

MyTable.where(\"my_id = ? \", n         


        
5条回答
  •  时光取名叫无心
    2021-02-05 03:48

    I suppose it's so called "rails magic" you can pass ranges

    Client.where(:created_at => (Time.now.midnight - 1.day)..Time.now.midnight)
    

    becomes

    SELECT * FROM clients WHERE (clients.created_at BETWEEN '2008-12-21 00:00:00' 
                                                        AND '2008-12-22 00:00:00')
    

    or if you pass a subset

    Client.where(:orders_count => [1,3,5])
    

    rails will do

    SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))
    

    more

提交回复
热议问题