Belongs_to question

Okay.

class User < AR
belongs_to :address
end

class Address < AR
has_one :user
end

So far, so good. But, now each user can have two addresses.

class User < AR
belongs_to :default_billing_address, :class => Address
belongs_to :default_shipping_address, :class => Address
end

And I added default_billing_address_id and default_shipping_address_id
to the User table. But it doesn’t seem to be working properly. I can:

u = User.find(x)
u.default_billing_address = Address.find(y)
u.save

But the default_billing_address_id field is never changed. Any ideas?

Thanks,
Joe

Hi!

Try this:
class User < AR
belongs_to :default_billing_address, :class => Address, :foreign_key
=>
“default_billing_address_id”
belongs_to :default_shipping_address, :class => Address, :foreign_key
=>
“default_shipping_address_id”
end

I think Rails supposes both adresses to be referenced with address_id.

Hope this helps,
Markus