0

I have these 3 models

  • Student
  • Gender
  • BloodType
  • Prefecture

Student has these three models such as Gender, BloodType, Prefecture. and each of them belongs to Student.

Association is set up already in each model files.

In this case, how can I code if I want to avoid N + 1 issue?

Is it something like this?

@students = Student.find(:all).includes.includes(:gender, :blood_type, :prefecture)
4

1 回答 1

3

You only need .includes once, and you can get rid of the find(:all) since Rails 3 does not require it.

students = Student.includes(:gender, :blood_type, :prefecture)

Having said that, it looks like these are just look up tables and I'd recommend checking out my gem ClassyEnum as a potential replacement if you are concerned about performance issues.

于 2013-07-18T01:21:09.940 回答