1

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?

basgys
  • 4,320
  • 28
  • 39
Zephyr4434
  • 796
  • 2
  • 13
  • 25

1 Answers1

1

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.

basgys
  • 4,320
  • 28
  • 39