2017-07-31 79 views
0

下面的代碼工作與導軌4.2.7完美的罰款,但有軌5+它拋出一個NameError l2_requirement.rb多個多態關聯與軌工作4軌沒有5

初始化常量「BonesReqPhoto」

模型

has_one :bones_req_photo, -> { where requireable_type: "BonesReqPhoto" }, 
    class_name: "ReqPhoto", foreign_key: :requireable_id, 
    foreign_type: :requireable_type 
has_one :bubbling_req_photo, -> { where requireable_type: "BubblingReqPhoto" }, 
    class_name: "ReqPhoto", foreign_key: :requireable_id, 
    foreign_type: :requireable_type 

模型req_photo.rb

belongs_to :requireable, polymorphic: true 

該模型使用一個多態型的關聯多次。 where約束用於區分兩者。

我用這個答案找出如何解決這個問題的軌道4,但很沮喪無濟於事試圖讓這對軌道5的工作:imageable type is not setting for polymorphic associations

什麼是做到這一點的正確方法導軌5?

回答

0

原來我誤解了原始指南。我試圖替換requireable_type,後來導致很多頭痛。

正確的答案是添加一個新列。我選擇了req_type

has_one :bones_req_photo, -> { where req_type: "BonesReqPhoto" }, 
    class_name: "ReqPhoto", as: :requireable 
has_one :bubbling_req_photo, -> { where req_type: "BubblingReqPhoto" }, 
    class_name: "ReqPhoto", as: :requireable