Forum: Ruby on Rails Weird associations, how would you do that ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
164578b8d21de6e92453bbbfa17260ba?d=identicon&s=25 Jérémy Kaz (jerefrer)
on 2009-05-20 21:43
(Received via mailing list)
Hi there,

I have some complex situation to solve, and can't manage to find a way
which satisfy me ...

Here's the problem :

You have to handle many Users (id, name, email, password).
You have a list of many Products (id, name) which are the same for all
users.
You have some Locations (id, user_id, name) of where to put one
product, each user defining his own locations.

BUT

Every user should be able to put his very own "physical instance" of
one product in a location OR NOT, which one will, consequently, be
different for every user. Moreover, he may specify an alternative name
for the product.

So you have to make some model/table that can link product, location,
and user altogether. Let's call it MyProducts.

IMO there is 2 ways to go, based on the above :
- either you put only the products that have a location in MyProducts
- or you duplicate (badbadbad!) all the products from Products in
MyProducts, and so for each user, which force you to maintain the same
list in both tables, but users and products won't change too often,
and it will require less work when you have to know which product has
been "modified" or not.

My way of solving this for the moment is the first one :

You have one Product model, that has_many user_products
You have one User model, that has_many user_products
You have one Location model, that has_many user_products
You have one MyProduct model, that belongs_to Product, belongs_to
Location, and belongs_to User

This doesn't seem too bad, does it ?

The problem is, in your index view, when you want to list all
Products, you have to get an array of all products, and an array of
all my_products, and then subtract the products that exist in
user_product from the products array, so you get only the product not
"modified" by the user, you can display them, and you can then display
those in user_product. But they're not ordered. So maybe there is some
more work to do so ...

And this looks ugly IMO.

So that's why I'm asking you, how would you do something like that ?
Is it a good idea to do as I did ? This looks like a three way
association, and I've been taught to never do that but when you can't
do anything else.

And above all, is there some Rails-way to do this ? Like using
has_and_belongs_to_many in some cases.

PS : I hope my English isn't too bad, I did my best ;)
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-20 22:05
(Received via mailing list)
Unless I am missing something (which is very likely) I do not see the
need
for the MyProducts table.  What is wrong with just

User
   has_many locations
Product
  has_many locations
Location
  belongs_to user
  belongs_to user

Then a user has a number of locations, each of which may have an
associated
product (or not).
Each product can appear in many or no locations.

Colin


2009/5/20 Jérémy <frere.jeremy@gmail.com>
A59b0daee5e5570e07a9f976721017e7?d=identicon&s=25 mike (Guest)
on 2009-05-20 23:01
(Received via mailing list)
2009/5/20, Colin Law <clanlaw@googlemail.com>:
>
>> Hi there,
>> product, each user defining his own locations.
>>
>> You have one Product model, that has_many user_products
>> user_product from the products array, so you get only the product not
>>
>> And above all, is there some Rails-way to do this ? Like using
>> has_and_belongs_to_many in some cases.
>>
>> PS : I hope my English isn't too bad, I did my best ;)
>>
>> >
>>
>
> >
>

--
Von meinen Mobilgerät aus gesendet
A59b0daee5e5570e07a9f976721017e7?d=identicon&s=25 mike (Guest)
on 2009-05-20 23:03
(Received via mailing list)
2009/5/20, Colin Law <clanlaw@googlemail.com>:
>
>> Hi there,
>> product, each user defining his own locations.
>>
>> You have one Product model, that has_many user_products
>> user_product from the products array, so you get only the product not
>>
>> And above all, is there some Rails-way to do this ? Like using
>> has_and_belongs_to_many in some cases.
>>
>> PS : I hope my English isn't too bad, I did my best ;)
>>
>> >
>>
>
> >
>

--
Von meinen Mobilgerät aus gesendet
164578b8d21de6e92453bbbfa17260ba?d=identicon&s=25 Jérémy Kaz (jerefrer)
on 2009-05-21 18:09
(Received via mailing list)
You actually are missing something, but I should have been more clear
on that part.

So, the hard point is that there is a generic list of products, shared
by all users. BUT this list is only for referencing purpose, as far as
a user may want a "modified" version of a product in this list.

Moreover, a "modified" product is not only a product associated to a
location, but it may have alternative captions as well.

An example may be more explicit :
Imagine you have 3 products, A, B, and C
And you have 2 users : John and Sally
And John have one location in his store : LocationJ
And Sally have one location in her store : LocationS
And John have put the product A in LocationJ
And Sally have put the product B in LocationS

When John wants to see the products list, he should see :
Code | Caption           | Location  | AlternativeCaption
  A    | The A Product | LocationJ | The Alt. A Product
==> But actually, in this table, the location is only an ID, pointing
to a location in the location table
  B    | The B Product |                |
  C    | The C Product |                |

And Sally should see :
Code | Caption           | Location  | AlternativeCaption
  A    | The A Product |                |
  B    | The B Product | LocationS | The Alt. B Product
  C    | The C Product |                |

So ok, I may use your model and put the alternative captions in the
Location table, but then it's not very relevant
This topic is locked and can not be replied to.