Ruby on rails 如何在多态表中为链式模型创建条目

Ruby on rails 如何在多态表中为链式模型创建条目,ruby-on-rails,polymorphic-associations,Ruby On Rails,Polymorphic Associations,我有课下作业 class A < ActiveRecord::Base has_many :b has_one :x, as: :xyz end class B < ActiveRecord::Base belongs_to :a has_many :c has_one :x, as: :xyz end class C < ActiveRecord::Base belongs_to :b has_many :d has_one :x, as

我有课下作业

class A < ActiveRecord::Base
  has_many :b 
  has_one :x, as: :xyz
end
class B < ActiveRecord::Base
  belongs_to :a
  has_many :c 
  has_one :x, as: :xyz
end
class C < ActiveRecord::Base
  belongs_to :b
  has_many :d 
  has_one :x, as: :xyz
end
class D < ActiveRecord::Base
  belongs_to :c
  has_one :x, as: :xyz
end
class X < ActiveRecord::Base
  # level :integer
  belongs_to :xyz, polymorphic: true
end
我创建了这个多态表来存储所有表的公共属性“Level”。我想做的是,如果A级被分配为1级,那么A级、B级、C级和D级也应该有1级。如果仅为B分配了级别=2;B、 C和D的级别应为2

我不知道如何设置这些值


谢谢

级别是多少?level是X的字段吗?那么如何分配级别?是的,级别是X的字段,用户为其设置值。通过所有模型表单中的嵌套_属性更新它。