I use the following query in SQL to find the same value in multiple fields of my model but would like to do it in a more correct Activerecord way.
MyModel.where("'some_value' in (a_field, another_field)").first.try(:id)
The following does not work since it's an AND and I need an OR
MyModel.where(a_field: 'some_value', another_field: 'some_value').first.try(:id)
Any suggestions ?
Out of curiosity: if I use the first (which works) and use a puts
or p
to view the result I see the results two times ? I wonder why..
EDIT in this example I use only two fields but in reality there could be more so an or wouldn't be doable and not dry