-
I have a few tables: ‘users’, ‘shops’, ‘addresses’, and ‘pictures’.
-
Users and Shops will both use the addresses and pictures tables. I
have
created several mapping tables:
addresses_shops
pictures_shops
addresses_users
pictures_users
-
Users CAN have many addresses and pictures (simple HABTM), but…
-
I’d like it if Shops only could have one of each. Since both users
and
shops are accessing addresses and pictures, I can’t exactly use a simple
‘has_one’ relationship with regards to Shops (…right?) -
As of right now, this is how I access the address of a shop:
@shop = Shop.find( id )
@address = @shop.addresses.first
…but I’d like to be able to do…
@shop = Shop.find( id )
@address = @shop.address
Is there a correct way to do this? I felt it was cleaner to seperate
duplicate address data into a single table and have tables reference it
as
necessary – but can this be done across HABTM and has_one
relationships?
Any advice/suggestions would be appriciated.
Thanks
Adam