I have a has_many through relationship between my cities and travels that looks like this:
class Travel < ActiveRecord::Base
has_many :cities, through: :destinations
end
class City < ActiveRecord::Base
has_many :travels, through: :destinations
end
And on my Travel
controller I have a method to join all its cities
names:
def city_names
self.cities.map { |e| e.name }.join(', ')
end
I installed the bullet gem and it tells me that this is a n+1 query and suggests that I add the includes
method on the query, but as far as I can tell this method will only work if I call it directly on the Travel
class.
e
; use something likecity
instead \$\endgroup\$includes
onself
in thecity_names
method? \$\endgroup\$(&:name)
instead of a block \$\endgroup\$