nested - Rails 4 has_many through - Cannot modify association -
i have dayitem model has 1 schoolprogram has many seminars.
class dayitem < activerecord::base has_one :school_program, dependent: :destroy has_many :seminars, through: :school_program accepts_nested_attributes_for :school_program accepts_nested_attributes_for :seminars, reject_if: :all_blank end class schoolprogram < activerecord::base belongs_to :day_item has_many :seminars, dependent: :destroy accepts_nested_attributes_for :seminars, allow_destroy: true, reject_if: :all_blank end class seminar < activerecord::base belongs_to :school_program end
i using cocoon gem dynamic nested forms follows.
_form.html.haml:
= simple_form_for [@day, @day_item] |f| = f.input :start_time = f.simple_fields_for :school_program |form| = form.input :school = form.simple_fields_for :seminars |seminar| = render 'seminar_fields', :f => seminar, :parent => form .links = link_to_add_association 'add seminar', form, :seminars
_seminar_fields.html.haml:
.nested-fields.well.well-compact .form-inline = f.input :name = link_to_remove_association "remove seminar", f
but when try add seminar following exception.
activerecord::hasmanythroughcantassociatethroughhasoneormanyreflection in etm::dayitemscontroller#update
cannot modify association 'dayitem#seminars' because source reflection class 'seminar' associated 'schoolprogram' via :has_many.
any appreciated.
circular reference in relationships (source reflection)
there may more 1 issue here, first 1 should addressed relationship seminars creates circular reference. declared in has_many in dayitem , has_one on schoolprogram, belongs parent class dayitem. please try change below our dayitem model. leave other models are, , let me know how goes.
class dayitem < activerecord::base has_one :school_program, dependent: :destroy accepts_nested_attributes_for :school_program end
Comments
Post a Comment