mardi 26 novembre 2019

ActiveRecord Associations(has_one) - Access parent object

class Parent < ApplicationRecord
  has_one: child
end


class Child < ApplicationRecord
  belongs_to :parent
end


childrens = Child.includes(:parent)

puts childrens.to_json
[{"id":1,"parent_id":1,"name":"Jack"},{"id":2,"parent_id":2,"name":"Oleg"}]

In this case, we can access parent object like this: child.parent

But it is not possible to access parent object in view. Is there any way to include parent objects in each child?

Thank you!

Aucun commentaire:

Enregistrer un commentaire