views:

36

answers:

2

I'm trying to do something like this, but it's not working. How would i do this in Rails 3?

Student.find(12).includes( :teacher )

A: 

You just have to be more careful with the order of the methods in this case:

Student.includes(:teacher).find(12)

rspeicher
that doesn't seem to be working.
99miles
Well what's it doing? You could always use the old way: `Student.find(12, :include => :teacher)`
rspeicher
It gets the record at id 12, but without the includes. The "old" way doesn't work in 3.0 anymore. I can get it to work this way Student.find_by_id(12).to_json( :include => :teacher) but that's not what i want
99miles
The old way works, it's just marked as deprecated.
rspeicher
OK, well it's not working for me here. So weird.
99miles
A: 

You could try "where" instead of "find":

Student.includes(:teacher).where(:id => 12)
Tim
;( nope. that doesn't work either
99miles
I tried something similar on one of my models and it worked in Rails 3, are you sure you have the "belongs_to" and "has_many" relationships set up?
Tim
Yes, it's very strange b/c it work fine when I put the include in the to_json method, so it seems like everything is set up fine.
99miles