Forum: Ruby on Rails has_one / HABTM relationship overlap - please advise

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Adam R. (Guest)
on 2005-11-28 17:46
(Received via mailing list)
1. I have a few tables: 'users', 'shops', 'addresses', and 'pictures'.

2. Users and Shops will both use the addresses and pictures tables. I
created several mapping tables:



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

4. I'd like it if Shops _only_ could have one of each. Since both users
shops are accessing addresses and pictures, I can't exactly use a simple
'has_one' relationship with regards to Shops (...right?)

5. 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
necessary -- but can this be done across HABTM and has_one
Any advice/suggestions would be appriciated.

Ruy A. (Guest)
on 2005-11-29 00:42
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.
This topic is locked and can not be replied to.