i have an Order model and a table for addresses. i was trying to use
single table inheritance so that i can store the addresses in the same
table.
here is my order model:
class Order < ActiveRecord::Base
has_one :billing_address, :class_name =>
‘BillingAddress’, :foreign_key => ‘order_id’
has_one :shipping_address, :class_name =>
‘ShippingAddress’, :foreign_key => ‘order_id’
end
and the address model:
class Address < ActiveRecord::Base
end
class BillingAddress < Address
belongs_to :order, :class_name => ‘Order’
end
class ShippingAddress < Address
belongs_to :order, :class_name => ‘Order’
end
everything appears to be working fine, unless i try and do something
like this:
my_order.billing_address.create(…)
i get this in the console:
NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.create
however, if i create an address this way:
BillingAddress.create(… :order => my_order) it works fine. i can
even use order.billing_address and it will pull up the right record.
is this right, or did i set up the relationships wrong?