Note that there are some explanatory texts on larger screens.

plurals
  1. POBest implementation of a multi-model association in rails?
    text
    copied!<p>Alright, a Rails Noob here, :D</p> <p>It looks like has__many :through is the latest greatest way to handle many to many relationships, but I am trying to keep this simple. Hopefully one of you guru's out there have handled this situation before:</p> <p>Here is the basic model setup I have now:</p> <pre><code>class User &lt; ActiveRecord::Base has_and_belongs_to_many :products end class Product &lt; ActiveRecord::Base has_and_belongs_to_many :users has_and_belongs_to_many :clients end class Client &lt; ActiveRecord::Base has_and_belongs_to_many :products end </code></pre> <p>Essentially, I have users in the system, that will have access (through association) to many different products that are being created, those products have many clients, but the clients can also be a part of many products, and the Products accessed by many users.</p> <p>All of the associations are working well, but now I want users to be able to add clients to their products, but only see clients that are associated with products they have access too.</p> <pre><code>Scenario: Given Bob has access to product A and B And does NOT have access to product C And and has clients on product B And wants to add them to product A. When in product A Bob should see clients from product B in his add list, And Bob should not see clients from product C </code></pre> <p>My noobish experience with rails fails to give me the experience on how to best build the array that will hold his client list.</p> <p>The way I am thinking be to use @bob.products to get the products Bob has access to then to .each those and find the clients associated with each product and then join them into a single array. But is this the BEST way?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload