1

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

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


class Purchase < ActiveRecord::Base
  has_one :shipping_address, -> { where(addressable_type: "shipping_address") }, class_name: Address, as: :addressable
  has_one :billing_address, -> { where(addressable_type: "billing_address") }, class_name: Address, as: :addressable
end

However, when I go to the console and try to build a new shipping address for a purchase, it doesn't generate the instance using my conditions. I thought -> { where(addressable_type: "shipping_address") } would prepopulate the addressable_type column with "shipping_address"?

Here's what I mean:

    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.build_shipping_address
    => #<Address id: nil, first_name: nil, last_name: nil, street_address: nil, street_address2: nil, zip_code: nil, phone_number: nil, created_at: nil, updated_at: nil, state_id: nil, city: nil, addressable_type: "Purchase", addressable_id: nil>
    irb(main):003:0> p.save
       (0.3ms)  SAVEPOINT active_record_1
      SQL (0.5ms)  INSERT INTO `purchases` (`created_at`, `updated_at`) VALUES ('2014-06-04 15:06:55', '2014-06-04 15:06:55')
      SQL (0.2ms)  INSERT INTO `addresses` (`addressable_id`, `addressable_type`, `created_at`, `updated_at`) VALUES (9, 'Purchase', '2014-06-04 15:06:55', '2014-06-04 15:06:55')
       (0.2ms)  RELEASE SAVEPOINT active_record_1
    => true
    irb(main):004:0> p.shipping_address
    => #<Address id: 10, first_name: nil, last_name: nil, street_address: nil, street_address2: nil, zip_code: nil, phone_number: nil, created_at: "2014-06-04 15:06:55", updated_at: "2014-06-04 15:06:55", state_id: nil, city: nil, addressable_type: "Purchase", addressable_id: 9>
    irb(main):005:0> p.reload
      Purchase Load (0.6ms)  SELECT  `purchases`.* FROM `purchases`  WHERE `purchases`.`id` = 9 LIMIT 1
    => #<Purchase id: 9, 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: "2014-06-04 15:06:55", updated_at: "2014-06-04 15:06:55", status: nil>
    irb(main):006:0> p.shipping_address
      Address Load (0.5ms)  SELECT  `addresses`.* FROM `addresses`  WHERE `addresses`.`addressable_id` = 9 AND `addresses`.`addressable_type` = 'Purchase' AND `addresses`.`addressable_type` = 'shipping_address' LIMIT 1
    => nil

Notice it doesn't set the addressable_type column. How would I fix this?

bigpotato
  • 26,262
  • 56
  • 178
  • 334
  • See my answer to your earlier question [here](http://stackoverflow.com/questions/24039964/rails-4-has-one-polymorphic-association-not-working) – j-dexx Jun 04 '14 at 15:14
  • 1
    You have a double where statement on the same column: `AND addresses.addressable_type = 'Purchase' AND addresses.addressable_type = 'shipping_address'` Your relation should be defined as `has_one :shipping_address, class_name: "Address", as: :addressable` (no where clause in there) – MrYoshiji Jun 04 '14 at 15:24

0 Answers0