samedi 27 juin 2015

Using Hash syntax with 'OR' for Where clause in Rails

I know there are three main types of syntax's for the where clause:

  1. Pure String
  2. Array
  3. Hash

Specifying AND for the where clause is straight forward:

#Pure String syntax
@people= Person.where("name = 'Neil' AND age = 27")

#Array syntax
@people = Person.where(["name = ? AND age = ?", 'Neil', 27])

#Hash syntax
@people = Person.where({name: "Neil", age: 27})

Specifying OR for this same where clause is stumping me for the hash syntax. Is it possible?

#Pure String syntax
@people= Person.where("name = 'Neil' OR age = 27")

#Array syntax
@people = Person.where(["name = ? OR age = ?", 'Neil', 27])

#Hash syntax DOESN'T WORK
@people = Person.where({name: "Neil" OR age: 27})

Aucun commentaire:

Enregistrer un commentaire