Ruby on rails 为什么';我的多态类型字段由rails自动分配

Ruby on rails 为什么';我的多态类型字段由rails自动分配,ruby-on-rails,ruby-on-rails-3,polymorphic-associations,single-table-inheritance,Ruby On Rails,Ruby On Rails 3,Polymorphic Associations,Single Table Inheritance,我将单表继承与多态关联结合使用。这是我的模型 class ChangeInformation < ActiveRecord::Base belongs_to :eventable, :polymorphic => true end class Race < ActiveRecord::Base has_many :track_condition_changes, :as => :eventable, :class_name => "ChangeInform

我将单表继承与多态关联结合使用。这是我的模型

class ChangeInformation < ActiveRecord::Base
  belongs_to :eventable, :polymorphic => true
end

class Race < ActiveRecord::Base
  has_many :track_condition_changes, :as => :eventable, :class_name => "ChangeInformation"
  #other associations omitted
end

class TrackConditionChange < ChangeInformation

end
当我使用以下创建方法时:

TrackConditionChange.create(:change_code => 1, :eventable_id => 3 :description => "test")
创建TrackConditionChange记录,填充类型字段,但不填充可事件类型字段(应为Race)。我的印象是rails自动填充这个字段,类似于STI类型字段。我是否有错误的印象,或者我的社交设置有问题


感谢您的输入。

如果您只传递可事件id,它如何知道它是什么类型?您必须传递整个可事件对象,或者根据跟踪\条件\更改关系构建它:

1。传递可事件对象:

race = Race.find(3)
TrackConditionChange.create(:change_code => 1, :eventable => race, :description => "test")
race = Race.find(3)
race.track_condition_changes << TrackConditionChange.new(:change_code => 1, :description => "test")
2。基于关系构建和保存:

race = Race.find(3)
TrackConditionChange.create(:change_code => 1, :eventable => race, :description => "test")
race = Race.find(3)
race.track_condition_changes << TrackConditionChange.new(:change_code => 1, :description => "test")
race=race.find(3)
race.track_条件_变化1,:description=>“test”)

如果您只传递可事件id,它如何知道它是什么类型?您必须传递整个可事件对象,或者根据跟踪\条件\更改关系构建它:

1。传递可事件对象:

race = Race.find(3)
TrackConditionChange.create(:change_code => 1, :eventable => race, :description => "test")
race = Race.find(3)
race.track_condition_changes << TrackConditionChange.new(:change_code => 1, :description => "test")
2。基于关系构建和保存:

race = Race.find(3)
TrackConditionChange.create(:change_code => 1, :eventable => race, :description => "test")
race = Race.find(3)
race.track_condition_changes << TrackConditionChange.new(:change_code => 1, :description => "test")
race=race.find(3)
race.track_条件_变化1,:description=>“test”)

Beerlington-谢谢你的帮助。不知何故,我确信rails可以从关联中找出类型,但经过进一步思考,我发现了问题所在。再次感谢!比灵顿-谢谢你的帮助。不知何故,我确信rails可以从关联中找出类型,但经过进一步思考,我发现了问题所在。再次感谢!