Rails嵌套属性/ inverse_of和STI

时间:2013-07-25 00:07:39

标签: ruby-on-rails nested-attributes single-table-inheritance

我一直在敲打这个问题。在同时保存新引荐(父对象)和约会(子)时会出现问题。我已经完成了与其他嵌套对象类似但似乎无法使其与单表继承 - 约会表一起使用。出于某种原因,inverse_of不会将新引荐的id传递给约会。

class Referral < ActiveRecord::Base

  has_many :appointments, class_name: 'Appointment::Base', inverse_of: :referral

  accepts_nested_attributes_for :appointments

end

class Appointment::Base < ActiveRecord::Base

  self.table_name = 'appointments'

  belongs_to :referral, inverse_of: :appointments

end
视图中的

fields_for :appointments do |a|

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

class_name子句中使用referral belongs_to怎么样?像这样:

class Appointment::Base < ActiveRecord::Base
  ...
  belongs_to :referral, class_name: 'Referral', inverse_of: :appointments
end

对于Base

,Rails可能与AppointmentReferral)在同一名称空间中查找