How to do find() with includes() in Rails 3
I'm trying to do something like this, but it's not working. How would I do this in R开发者_如何学JAVAails 3?
Student.find(12).includes(:teacher)
You just have to be more careful with the order of the methods in this case:
Student.includes(:teacher).find(12)
Old question I know but just in case this helps someone...
Doing something like @student = Student.includes(:teacher).where(:id => 12)
returns an array and so then using something such as @student.id
doesn't work.
Instead you could do:
@student = Student.includes(:teacher).where(:id => 12).first
Although Student.includes(:teacher).find(12)
should work, but you can use the where
version if you need to search by other/multiple fields.
You could try "where" instead of "find":
Student.includes(:teacher).where(:id => 12)
精彩评论