rails 5 中的多个嵌套表单
Multiple nested forms in rails 5
我正在研究嵌套属性,我对模型之间的关系有一些疑问。
假设我有 Show has_many Seasons。 Show 和 Season 都可以有很多 Actor 和 Staff .参见 table 示例:
创建 Show 时,Season 接受 Show 关联并且 Actors 和 Staff 接受 Show 和 Season 属性。这会是三重嵌套形式吗?
所以我的模型看起来像这样?
class Show < ApplicationRecord
has_many :seasons, dependent: :destroy
has_many :actors, dependent: :destroy
has_many :staff, dependent: :destroy
accepts_nested_attributes_for :seasons, allow_destroy: true
accepts_nested_attributes_for :actors, allow_destroy: true
accepts_nested_attributes_for :staff, allow_destroy: true
end
class Season < ApplicationRecord
belongs_to :show
has_many :actors, dependent: :destroy
has_many :staffs, dependent: :destroy
accepts_nested_attributes_for :actors, allow_destroy: true
accepts_nested_attributes_for :staff, allow_destroy: true
end
class Actor < ApplicationRecord
belongs_to :show
belongs_to :season
end
class Staff < ApplicationRecord
belongs_to :show
belongs_to :season
end
我的 Show Controller 看起来像:
class ShowsController < ApplicationController
def create
@show.seasons.build
@show.seasons.build.actors.build
@show.seasons.build.staffs.build
end
end
正确的架构是:
class Show < ApplicationRecord
has_many :seasons, dependent: :destroy
has_many :actors, dependent: :destroy
has_many :staff, dependent: :destroy
accepts_nested_attributes_for :seasons, allow_destroy: true
accepts_nested_attributes_for :actors, allow_destroy: true
accepts_nested_attributes_for :staff, allow_destroy: true
end
class Season < ApplicationRecord
belongs_to :show
end
class Actor < ApplicationRecord
belongs_to :show
belongs_to :season
end
class Staff < ApplicationRecord
belongs_to :show
belongs_to :season
end
规则很简单 - 如果 table objects
有一个外键 subject_id
,它链接到 table subjects
,那么模型 Object
可能包含 belongs_to :subject
关联。
有一个很好的 explanation 关于根据 foreign_key 放置在模型中使用哪种关系。
题外话:
我不知道你项目的具体情况,但看起来有点冒犯。每次创建新节目时,您都会创建新的演员和工作人员,就像他们是消耗品一样。我不想显得愤世嫉俗,但演员和工作人员是可以重用的。如果是这样,创建 has_and_belongs_to_many 关系可能是合理的。
我正在研究嵌套属性,我对模型之间的关系有一些疑问。
假设我有 Show has_many Seasons。 Show 和 Season 都可以有很多 Actor 和 Staff .参见 table 示例:
创建 Show 时,Season 接受 Show 关联并且 Actors 和 Staff 接受 Show 和 Season 属性。这会是三重嵌套形式吗?
所以我的模型看起来像这样?
class Show < ApplicationRecord
has_many :seasons, dependent: :destroy
has_many :actors, dependent: :destroy
has_many :staff, dependent: :destroy
accepts_nested_attributes_for :seasons, allow_destroy: true
accepts_nested_attributes_for :actors, allow_destroy: true
accepts_nested_attributes_for :staff, allow_destroy: true
end
class Season < ApplicationRecord
belongs_to :show
has_many :actors, dependent: :destroy
has_many :staffs, dependent: :destroy
accepts_nested_attributes_for :actors, allow_destroy: true
accepts_nested_attributes_for :staff, allow_destroy: true
end
class Actor < ApplicationRecord
belongs_to :show
belongs_to :season
end
class Staff < ApplicationRecord
belongs_to :show
belongs_to :season
end
我的 Show Controller 看起来像:
class ShowsController < ApplicationController
def create
@show.seasons.build
@show.seasons.build.actors.build
@show.seasons.build.staffs.build
end
end
正确的架构是:
class Show < ApplicationRecord
has_many :seasons, dependent: :destroy
has_many :actors, dependent: :destroy
has_many :staff, dependent: :destroy
accepts_nested_attributes_for :seasons, allow_destroy: true
accepts_nested_attributes_for :actors, allow_destroy: true
accepts_nested_attributes_for :staff, allow_destroy: true
end
class Season < ApplicationRecord
belongs_to :show
end
class Actor < ApplicationRecord
belongs_to :show
belongs_to :season
end
class Staff < ApplicationRecord
belongs_to :show
belongs_to :season
end
规则很简单 - 如果 table objects
有一个外键 subject_id
,它链接到 table subjects
,那么模型 Object
可能包含 belongs_to :subject
关联。
有一个很好的 explanation 关于根据 foreign_key 放置在模型中使用哪种关系。
题外话:
我不知道你项目的具体情况,但看起来有点冒犯。每次创建新节目时,您都会创建新的演员和工作人员,就像他们是消耗品一样。我不想显得愤世嫉俗,但演员和工作人员是可以重用的。如果是这样,创建 has_and_belongs_to_many 关系可能是合理的。