ruby on rails - How can I code if I want to avoid n plus one issue? -
i have these 3 models
- student
- gender
- bloodtype
- prefecture
student has these 3 models such gender, bloodtype, prefecture. , each of them belongs student.
association set in each model files.
in case, how can code if want avoid n + 1 issue?
is this?
@students = student.find(:all).includes.includes(:gender, :blood_type, :prefecture)
you need .includes
once, , can rid of find(:all)
since rails 3 not require it.
students = student.includes(:gender, :blood_type, :prefecture)
having said that, looks these tables , i'd recommend checking out gem classyenum potential replacement if concerned performance issues.
Comments
Post a Comment