So che ci sono 3 notazioni principali per fornire argomenti al metodo where
ActiveRecord:ActiveRecord O interrogazione Hash notazione
- pura stringa
- Array
- Hash
Specifica and
per la Il metodo where
è diretto:
# Pure String notation
Person.where("name = 'Neil' AND age = 27")
# Array notation
Person.where(["name = ? AND age = ?", 'Neil', 27])
# Hash notation
Person.where({name: "Neil", age: 27})
Specificare or
per questo stesso metodo where
mi sta bloccando per la sintassi dell'hash. È possibile?
# Pure String notation
Person.where("name = 'Neil' OR age = 27")
# Array notation
Person.where(["name = ? OR age = ?", 'Neil', 27])
# Hash notation DOESN'T WORK
Person.where({name: "Neil" OR age: 27})
Questa è una risposta completa. La risposta di 'arel' è particolarmente buona poiché questo è ciò che viene aggiunto a Rails 5, come ho capito. –
Se non si riesce ad attendere Rails 5, è possibile utilizzare questo backport della funzione alle guide 4.2. Ben confezionato come il 'dove-o' gemma https://github.com/Eric-Guo/where-or – Rob
Se il tuo progetto è ancora in Rails 3+, puoi provare i rails_or gem: https: // github. com/khiav223577/rails_or –