wiem, że istnieją 3 główne zapisy na dostarczanie argumentów metody where
ActiveRecord:ActiveRecord OR Hash zapytania notacja
- Czysta String
- Array
- Hash
Określanie and
dla Metoda jest prosta:
# 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})
Podanie or
dla tej samej metody where
obciąża mnie hashową składnią. Czy to możliwe?
# 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})
Jest to pełna odpowiedź. Odpowiedź "arel" jest szczególnie dobra, ponieważ to właśnie dodaje się do Rails 5, jak rozumiem. –
Jeśli nie możesz czekać na Rails 5, możesz użyć tego backportu funkcji do szyn 4.2. Ładnie zapakowany jako "gdzie-lub" klejnot https://github.com/Eric-Guo/where-lub – Rob
Jeśli twój projekt jest jeszcze w Railsach 3+, możesz wypróbować rails_or gem: https: // github. com/khiav223577/rails_or –