Listing cities in an itinerary I have a has_many through relationship between my cities and travels that looks like this:

class Travel &lt; ActiveRecord::Base
  has_many :cities, through: :destinations
end
class City &lt; 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.
You can use includes on any activerecord relation.

Judging by your message about having N+1 queries I suspect you are calling city_names somewhere in a loop. then the correct placement of the includes is the place where you select the elements to loop past.

I'm guessing it is for a view or something so i write some example code for .html.erb.

In controller:

@travels = Travels.all.includes(destinations: :cities)


In view:

&lt;% @travels.each do |travel| %&gt;
  ... Some code displaying travel info
  &lt;%= travel.city_names %&gt;
  ... More code displaying travel info
&lt;% end %&gt;


And a suggestion in line with what Mark Thomas wrote as a comment. You can refactor you city_names method for more readable code, either according to his suggestion or like this:

def city_names
  self.cities.map(&amp;:name).join(', ')
end