lundi 18 janvier 2016

Creating nested records on create with Rails

I have a projects model that I am using to auto generate departments within a specific project on create. This is included in the projects model with:

class Project < ActiveRecord::Base
    attr_accessible :title, :departments_attributes, :positions_attributes, :id
    belongs_to :user
    has_many :departments
    has_many :positions
    validates :title, presence: true

    before_create :set_departments
    accepts_nested_attributes_for :departments
    accepts_nested_attributes_for :positions

    private
       def set_departments
            self.departments.build department: "Test Dept", production_id: self.id

       end

end

Each department has many positions. I am trying to create positions as well for the departments. How could I associate a new position with a department in this model?

Aucun commentaire:

Enregistrer un commentaire