Rails 4: Has_one polymorphic association not working

后端 未结 3 821
耶瑟儿~
耶瑟儿~ 2020-12-20 06:52

I have two models Purchase and Address. I\'m trying to make Address polymorphic so I can reuse it in my Purchase model fo

3条回答
  •  孤城傲影
    2020-12-20 07:36

    You need to specify the :class_name option for the has_one association, as the class name can't be inferred from the association name i.e., :shipping_address and :billing_address in your case doesn't give an idea that they refer to class Address.

    Update the Purchase model as below:

    class Purchase < ActiveRecord::Base
      has_one :shipping_address, class_name: "Address", as: :addressable
      has_one :billing_address, class_name: "Address", as: :addressable
      ## ...
    end
    

提交回复
热议问题