What is the syntax for like in Ruby on Rails? This is something I'm trying to do:
I am trying to find all the last name from table which starts with egm so something like %egm%
. I know how to do using find_by_sql but just curious to know the Ruby way.
s = Person.find_by_last_name('nan%')
Ruby compares the object in the when clause with the object in the case clause using the === operator. For example, 1.. 5 === x , and not x === 1..
=~ is Ruby's basic pattern-matching operator. When one operand is a regular expression and the other is a string then the regular expression is used as a pattern to match against the string. (This operator is equivalently defined by Regexp and String so the order of String and Regexp do not matter.
With an if statement you can check if something is true . But when you want to check for the opposite “not true” (false) there is two things you can do. You can reverse the value with ! . Remember, using unless in Ruby is just the reverse of using if.
Ruby | Array class first() function first() is a Array class method which returns the first element of the array or the first 'n' elements from the array.
Person.where('name LIKE ?', '%egm%').all
l_name_var = "nan"
Person.where("people.last_name LIKE :l_name", {:l_name => "#{l_name_var}%"})
or in your case
l_name_var = "egm"
Person.where("people.last_name LIKE :l_name", {:l_name => "%#{l_name_var}%"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With