Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - ActiveRecord includes for multiple levels

See associations below:

class Continent < ActiveRecord::Base
    has_many :countrys

class Country < ActiveRecord::Base
    belongs_to :continent
    has_many :addresses

class Address < ActiveRecord::Base
    belongs_to :person
    belongs_to :street

class Person < ActiveRecord::Base
    has_many :addresses

How can I start with Person.includes() and include associations all the way up to the continent when querying the db.

I've been able to include up to the country via .includes(addresses: :country), however it seems not to get the last level.

like image 292
PJ Martins Avatar asked Nov 18 '14 02:11

PJ Martins


1 Answers

You can add 1 more level of hash:

.includes(addresses: {country: :continent})

like image 177
Vu Minh Tan Avatar answered Sep 21 '22 14:09

Vu Minh Tan