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]> > > 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 ;) > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

