Weird associations, how would you do that?

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 :wink:

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 [email protected]

2009/5/20, Colin L. [email protected]:

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 :wink:


Von meinen Mobilgerät aus gesendet

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

2009/5/20, Colin L. [email protected]:

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 :wink:


Von meinen Mobilgerät aus gesendet