多态关系——存储在type属性中的内容



假设你有一个这样的多态关系:

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

假设您有一个id为1的@product_invoice,并且您有一个属于该产品的@picture。我知道@图片。Imagable_id应该等于1,但是@picture中存储的值。imagable_type ?

  • "ProductInvoice"
  • "ProductInvoices"
  • "product_invoice"
  • "product_invoices"

via G.B in comments

"ProductInvoice"

相关内容

最新更新