开发者

How to do find() with includes() in Rails 3

开发者 https://www.devze.com 2023-01-20 07:23 出处:网络
I\'m trying to do something like this, but it\'s not working. How would I do this in R开发者_如何学JAVAails 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)
0

精彩评论

暂无评论...
验证码 换一张
取 消