activerecord - Parent object eager load nested associations Rails -


in rails 4 have following models

class parent < activerecord::base  has_many :sons  has_many :grand_sons, through: sons   scope :load_tree, (id) -> {parent.includes(sons: [:grand_sons]).find(id)} end  class son < activerecord::base   belongs_to :parent   has_many :grand_sons end  class grandson < activerecord::base   belongs_to :son end 

with load_tree obtain object like:

{    id: 1,    sons: [           {              id: 1,              parent_id: 1,              grand_sons:[ { id: 1, son_id: 1, } , ...]           }, ...         ]  } 

but after doing tree = parent.load_tree(1) got tree # <parent id=1> tree.sons #[<son id=1>, <son id =2>] seems not able eager load objects, suggestions?

try

parent.includes(:sons, {:sons => :grand_sons}).find(id) 

my understanding is, must specify relationship nested associations explicitly.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -