Has_one / HABTM relationship overlap - please advise


#1
  1. I have a few tables: ‘users’, ‘shops’, ‘addresses’, and ‘pictures’.

  2. 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

  1. Users CAN have many addresses and pictures (simple HABTM), but…

  2. 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?)

  3. 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


#2

Perhaps I’m not understanding this, but why do shops-address and
shops-pictures need habtm?

Seems like pictures/addresses has_many shops and shops belongs_to
picture/addresses, no?
So you have a shop_id fkey in the pictures and addresses table.

This won’t affect the habtm relationship to users afaik.