【问题标题】:Rails Nested Attributes / inverse_of and STIRails 嵌套属性 / inverse_of 和 STI
【发布时间】:2013-07-25 00:07:39
【问题描述】:

我一直在努力解决这个问题。同时保存新的推荐(父对象)和约会(子对象)时会出现问题。我对其他嵌套对象做过类似的事情,但似乎无法让它与单表继承——约会表一起工作。由于某种原因, 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|

感谢任何帮助。

【问题讨论】:

  • 你现在解决了吗?

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


【解决方案1】:

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

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

Rails 可能在与Base (Appointment) 相同的命名空间中查找Referral

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多