I think this is a simple question:
I have a Building model and each Building has_one city and each city belongs_to Buildings. I'd like to perform a search on the Building's city name (not the id) using Squeel.
Currently I have:
where{(zip =~ "%#{search}%") | (Building.all.map(&:city) =~ "%#{search}%")}
The searching through zip codes works fine since that is a direct attribute for the Building model. It's the second clause I am struggling with.
Right now, I get this error:
Cannot visit NilClass
How Can I make this work?
You just need to join the city table to filter on the city field.
where{(zip =~ "%#{search}%") | (city.name =~ "%#{search}%")}.joins{city.outer}
If all buildings have one city you can remove the outer join.