relations of various models in rails

1

Good morning, I have 3 models "Empleado", "grupo_familar" and "documentos_familiares". Employee has a one_to-many relationship with grupo_familar and grupo_familiar a one_to_many relationship with family_files. When wanting to collect employee data I get the error: undefined method 'document_families' for ActiveRecord :: Associations :: CollectionProxy [] > EL code ...

#modelo empleado
class Employee < ApplicationRecord
    has_many :family_groups

    accepts_nested_attributes_for :family_groups,  allow_destroy: true

    mount_uploader :avatar, ImageUploader
    mount_uploader :copy_document, CopyDocumentUploader
end

#modelo grupo_familiar 
class FamilyGroup < ApplicationRecord
    belongs_to :employee, optional: true
    has_many :document_families
    accepts_nested_attributes_for :document_families,  allow_destroy: true
end

#modelo documento_familiares
class DocumentFamily < ApplicationRecord
    belongs_to :family_group, optional: true
end

#en empleado controller
  def show
      @employee.family_groups # de esta forma me trae los grupos familiares con exito
      @emmployee.family_groups..document_families #me sale el error anteriormente comentado.
  end

The intention is to collect the data of the employee, the family group and the documents of each family group.

    
asked by sandovalgus 15.11.2016 в 15:47
source

1 answer

1

What you need is to call has_many :through :

class Employee < ApplicationRecord
    has_many :family_groups
    has_many :document_families, through: :family_groups

Then simply call:

@employee.document_families
    
answered by 15.11.2016 / 16:52
source