I have a model:
class Merchant < ActiveRecord::Base
belongs_to :billing_address, class_name: Address, dependent: :destroy
belongs_to :other_address1, class_name: Address, dependent: :destroy
belongs_to :other_address2, class_name: Address, dependent: :destroy
belongs_to :other_address3, class_name: Address, dependent: :destroy
belongs_to :other_address4, class_name: Address, dependent: :destroy
...
end
Address has no associations.
When I do this:
merchant.billing_address.destroy
In the database, the address record is gone, but merchants.billing_address_id keeps a bogus value. This is mysql, so no referential integrity.
What am I doing wrong?
NOTE: I realize this might be better modeled as a has_one association. I might have to go there, but I prefer not to.
UPDATE: Added a little more code to show the multiple Address associations.
It turns out that ActiveRecord does not support one-sided belongs_to associations, unlike other ORMs I’ve worked with. But you can hack it yourself like so: