0

I'm having an N+1 query issue with the following active record query...

@interviews.each do |interview|

I want to solve it by adding :college_play_histories to the active record query through using .includes(:college_play_histories), but currently that association doesn't exist between interviews and college_play_histories. I was going to setup an association between Interview and CollegePlayHistory, but this seemed incorrect to me and I wasn't sure if there was a way to specify the college_play_histories in the includes through player.

to @interviews.includes(:college_play_history).each do |interview|

Here are my current models and associations:

Interview

class Interview < ActiveRecord::Base
  belongs_to :player
end

Player

class Player < ActiveRecord::Base
  has_many :interviews
  has_many :college_play_histories
end

CollegePlayHistory

class CollegePlayHistory < ActiveRecord::Base
  belongs_to :player
end
daveomcd
  • 6,367
  • 14
  • 83
  • 137
  • 1
    seems to me `has_many through:..` will solve it – Arup Rakshit Jun 02 '15 at 17:09
  • which is correct? `has_many through:` or `.includes(player: :college_play_histories)` ? I hesitate on the `has_many through:` because the models are not in my opinion really related in the sense of accessing play histories from interview directly. Hopefully I've conveyed this properly... – daveomcd Jun 02 '15 at 17:12
  • why not use `includes(player: :college_play_histories)`? – AbM Jun 02 '15 at 17:14
  • 1
    This might come in handy in the future: https://github.com/flyerhzm/bullet – NM Pennypacker Jun 02 '15 at 17:15
  • @AbM I was unaware of it before posting the question. I think it is the way to go unless someone can detail otherwise. – daveomcd Jun 02 '15 at 17:15
  • 1
    http://stackoverflow.com/a/24397716/2697183 – AbM Jun 02 '15 at 17:17

0 Answers0