本文介绍了Rails 4:Has_one 多态关联不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型PurchaseAddress.我正在尝试使 Address 多态,以便我可以在 has_one :billing_addresshas_one :shipping_addressPurchase 模型中重用它/代码>.这是我的架构:

I have two models Purchase and Address. I'm trying to make Address polymorphic so I can reuse it in my Purchase model for has_one :billing_address and has_one :shipping_address. Here's my schema:

create_table "addresses", force: true do |t|
  t.string   "first_name"
  t.string   "last_name"
  t.string   "street_address"
  t.string   "street_address2"
  t.string   "zip_code"
  t.string   "phone_number"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "state_id"
  t.string   "city"
  t.string   "addressable_type" #<-- 
  t.integer  "addressable_id"   #<--
end

地址模型:

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true
  ...
end

购买模式:

class Purchase < ActiveRecord::Base
  has_one :shipping_address, as: :addressable
  has_one :billing_address, as: :addressable
  ...
end

我觉得一切都很好,但我的 Rspec 测试失败了:

Everything looks fine to me, but my Rspec tests fail:

    Failures:

      1) Purchase should have one shipping_address
         Failure/Error: it { should have_one(:shipping_address) }
           Expected Purchase to have a has_one association called shipping_address (ShippingAddress does not exist)
         # ./spec/models/purchase_spec.rb:22:in `block (2 levels) in <top (required)>'

      2) Purchase should have one billing_address
         Failure/Error: it { should have_one(:billing_address) }
           Expected Purchase to have a has_one association called billing_address (BillingAddress does not exist)
         # ./spec/models/purchase_spec.rb:23:in `block (2 levels) in <top (required)>'

似乎没有检测到关联是多态的.即使在我的控制台中:

It doesn't seem to be detecting that the association is polymorphic. Even in my console:

    irb(main):001:0> p = Purchase.new
    => #<Purchase id: nil, user_id: nil, order_date: nil, total_cents: 0, total_currency: "USD", shipping_cents: 0, shipping_currency: "USD", tax_cents: 0, tax_currency: "USD", subtotal_cents: 0, subtotal_currency: "USD", created_at: nil, updated_at: nil, status: nil>
    irb(main):002:0> p.shipping_address
    => nil
    irb(main):003:0> p.build_shipping_address
    NameError: uninitialized constant Purchase::ShippingAddress

我做错了什么??

推荐答案

您需要为 has_one 关联指定 :class_name 选项,因为类名可以'不能从关联名称中推断出,即 :shipping_address:billing_address 在您的情况下并没有给出它们指的是 Address 类的想法.

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.

更新Purchase模型如下:

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

这篇关于Rails 4:Has_one 多态关联不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 18:38