【问题标题】:A polymorphic object that belongs to a quasi-STI object: object_type is incorrect属于准STI对象的多态对象:object_type不正确
【发布时间】:2009-10-13 23:13:11
【问题描述】:

考虑:

class Person < ActiveRecord::Base
  class << self
    def setup
      has_one :address, :as => :addressable
    end
  end
end

class Employee < Person
  setup
end

class Address < ActiveRecord::Base
  belongs_to :addressable, :polymorphic => true
end

# Shouldn't this be 'Employee'? Is it possible to override?
Employee.create.address.create.addressable_type == 'Person'

编辑:我在那里迷惑了一阵子。这不是真正的 STI,它只是继承,因为 Employee 有自己的表。

谢谢!

【问题讨论】:

    标签: ruby-on-rails polymorphic-associations sti


    【解决方案1】:

    Bingo:

    class Person < ActiveRecord::Base
      self.abstract_class = true
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-04
      • 2010-10-20
      • 2013-03-25
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多