多态关系 - 什么存储在type属性中?

时间:2014-06-24 04:10:33

标签: ruby-on-rails-4 polymorphism has-many polymorphic-associations belongs-to

让我们说你有这样的多态关系:

class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
end

class Employee < ActiveRecord::Base
  has_many :pretty_pictures, as: :imageable
end

class ProductInvoice < ActiveRecord::Base
  has_many :pretty_pictures, as: :imageable
end

这是你对图片模型的迁移:

class CreatePictures < ActiveRecord::Migration
  def change
    create_table :pictures do |t|
      t.string :name
      t.references :imageable, polymorphic: true
      t.timestamps
    end
  end
end

我们假设你有一个@product_invoice,其中:id为1,你有一张属于这个产品的@picture。我知道@ picture.imagable_id应该等于1,但@ picture.imagable_type中存储的值是什么?

  • &#39; ProductInvoice&#39;
  • &#39; ProductInvoices&#39;
  • &#39; product_invoice&#39;
  • &#39; product_invoices&#39;

1 个答案:

答案 0 :(得分:0)

通过G.B评论

'ProductInvoice'

相关问题