让我们说你有这样的多态关系:
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中存储的值是什么?
答案 0 :(得分:0)
通过G.B评论
'ProductInvoice'