0

比方說,你有這樣的多態關係:多態關係 - 存儲在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 

假設你有一個一@product_invoice:1 ID和你有一個@picture屬於這個產品。我知道@ picture.imagable_id應該等於1,但是存儲在@ picture.imagable_type中的值是什麼?

  • 'ProductInvoice'
  • 'ProductInvoices'
  • 'product_invoice'
  • 'product_invoices'
+1

'ProductInvoice' –

回答

0
通過G.B

在評論

'ProductInvoice'