Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching second level model with rails active record

I have a relationship in which a post belongs to the city which inturn belongs to a state like:

class Post < ActiveRecord::Base
  belongs_to :city
end
class City < ActiveRecord::Base
  belongs_to :state
end

Now i want to find all the posts along with the their cities and the states to which the belong to. I wrote the following query to fetch the posts with their cities but out of ideas on how to fetch the corresponding state with the city in the same finder:

@post = Post.find :all, :include => [:city]

Any help is appreciated.

Thanks.

like image 578
Ishu Avatar asked Jan 21 '10 13:01

Ishu


1 Answers

Post.all( :include => { :city => :state })
like image 147
Farrel Avatar answered Sep 21 '22 06:09

Farrel