Rails 4 has_many 通过 - 无法修改关联
Rails 4 has_many through - Cannot modify association
我有一个 DayItem 模型,它有一个包含许多研讨会的 SchoolProgram。
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
我正在使用 Cocoon gem 动态嵌套表单,如下所示。
_form.html.haml:
= simple_form_for [@day, @day_item] do |f|
= f.input :start_time
= f.simple_fields_for :school_program do |form|
= form.input :school
= form.simple_fields_for :seminars do |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
但是当我尝试添加研讨会时出现以下异常。
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection in Etm::DayItemsController#update
Cannot modify association 'DayItem#seminars' because the source reflection class 'Seminar' is associated to 'SchoolProgram' via :has_many.
感谢任何帮助。
关系中的循环引用(源反射)
这里可能存在多个问题,但第一个应该解决的问题是您与研讨会的关系创建了循环引用。它在 DayItem 中的 has_many 中声明,然后在 SchoolProgram 中声明为 has_one,SchoolProgram 本身属于父 class DayItem。请尝试对我们的 DayItem 模型进行以下更改。其他模型保持原样,让我知道进展如何。
class DayItem < ActiveRecord::Base
has_one :school_program, dependent: :destroy
accepts_nested_attributes_for :school_program
end
我有一个 DayItem 模型,它有一个包含许多研讨会的 SchoolProgram。
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
我正在使用 Cocoon gem 动态嵌套表单,如下所示。
_form.html.haml:
= simple_form_for [@day, @day_item] do |f|
= f.input :start_time
= f.simple_fields_for :school_program do |form|
= form.input :school
= form.simple_fields_for :seminars do |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
但是当我尝试添加研讨会时出现以下异常。
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection in Etm::DayItemsController#update
Cannot modify association 'DayItem#seminars' because the source reflection class 'Seminar' is associated to 'SchoolProgram' via :has_many.
感谢任何帮助。
关系中的循环引用(源反射)
这里可能存在多个问题,但第一个应该解决的问题是您与研讨会的关系创建了循环引用。它在 DayItem 中的 has_many 中声明,然后在 SchoolProgram 中声明为 has_one,SchoolProgram 本身属于父 class DayItem。请尝试对我们的 DayItem 模型进行以下更改。其他模型保持原样,让我知道进展如何。
class DayItem < ActiveRecord::Base
has_one :school_program, dependent: :destroy
accepts_nested_attributes_for :school_program
end