Note that there are some explanatory texts on larger screens.

plurals
  1. POModeling Complex Relationships in Rails
    primarykey
    data
    text
    <p><h1>UPDATE</H1> OK, I figured it out. I had to call the following for varieties: </p> <pre><code>&lt;%=h @user.varieties.find_by_product_id(product.id).name %&gt; </code></pre> <p>Here's my two follow questions: </p> <p>(1) Is this going to cause problems when editing/deleting records because I'm not calling the join model? I've seen some Ryan Bates videos where he stresses this point, but I'm having trouble trying to reference the join model here. In other words, should the above code by called through <code>user_products</code>?</p> <p>If I use the following code, which references the join table, I'm only able to get it to display the <code>variety_id</code> from the join table (since there's no name column for varieties in the join table). I'm not sure how to make this code reference the <code>variety_id</code> in the join table, but then go to the variety table to get the actual name of the variety from the "name" column. </p> <pre><code>&lt;% @user.products.each do |product| %&gt; &lt;% @user.user_products.find_by_product_id(product.id).variety_id %&gt; &lt;% end %&gt; </code></pre> <p>(2) Is this complex stuff properly placed in the view layer, or is there a better way to move it to the model or controller?</p> <p>Thanks. </p> <h2>Original question below is now solved ...</h2> <p>I have the following models:<br> - users<br> - products<br> - varieties - user_products</p> <p>Here's the real world version of what I'm trying to do. Let's say User is a grocery store. Products are fruits, like apples. And varieties are types of apples, like fuji and mcintosh. </p> <p>I need to create an app where: </p> <ul> <li><p>A user can add many types of products to his page. The products can include multiple varieties, but the user is NOT required to include any varieties. For example, Topps Grocery Store can add apples to their page. If that's all they want to display, that needs to be ok. However, they can also add more detail by including the types of apples they carry, like fuji, mcintosh, etc. The variety can't just be a detailed product. In other words, I can't make each product be something like apple - fuji, apple - mcintosh. They need to be two separate models. </p></li> <li><p>On the user's page (i.e. the "show" view), I need to be able to display both the product and the variety (if any). The system needs to understand that the varieties are connected to the particular product for this particular user. </p></li> </ul> <p>Following the first answer I received, I revised my models as described in the answer below. Each variety belongs to one product, i.e. fuji belongs to only the apple product, which is a distinct id in the product table. And, a product has many varieties, i.e. the apple product might have 5 or 10 different varieties. </p> <p>However, it gets more complicated because each user might have a different set of product/variety combinations. For example, Topps grocery store (user) might have apples (product) that are fuji and mcintosh (varieties). But, Publix grocery store (user) might have apples (product) that are red delicious and gala (varieties). </p> <p>On the user page, I want to be able to call up each product the user carries and then display the varieties connected to each of those products if the user has chosen any varieties.</p> <p>When I try the code listed below in the show view, I get the following error: undefined method `user_product' for #: </p> <pre><code>&lt;% @user.products.each do |product| %&gt; &lt;% @user.user_products.find_by_product_id(product.id).varieties %&gt; &lt;% end %&gt; </code></pre> <p>On the other hand, when I try the other option you gave me (listed below), the page loads and the sql query seems right in the log, but no varieties display on the page, which is weird because I triple checked and there are records in the database that should match the query. Details below... </p> <pre><code>&lt;% @user.products.each do |product| %&gt; &lt;% @user.varieties.find_by_product_id(product.id) %&gt; &lt;% end %&gt; </code></pre> <p>This code runs the following sql query: </p> <p><code>User Load (0.7ms) SELECT * FROM "users" WHERE ("users"."id" = 2)</code></p> <p><code>Variety Load (0.5ms) SELECT "varieties".* FROM "varieties" INNER JOIN "user_product" ON "varieties".id = "user_products".variety_id WHERE (("seasons".user_id = 2))</code></p> <p>Rendering template within layouts/application<br> Rendering users/show </p> <p><code>Product Load (0.7ms) SELECT "products".* FROM "products" INNER JOIN "user_product" ON "products".id = "user_products".product_id WHERE (("user_products".user_id = 2))</code></p> <p><code>Variety Load (0.4ms) SELECT "varieties".* FROM "varieties" INNER JOIN "user_product" ON "varieties".id = "user_products".variety_id WHERE ("varieties"."product_id" = 1) AND (("user_products".user_id = 2)) LIMIT 1</code></p> <p><code>Variety Load (0.2ms) SELECT "varieties".* FROM "varieties" INNER JOIN "user_products" ON "varieties".id = "user_products".variety_id WHERE ("varieties"."product_id" = 2) AND (("user_products".user_id = 2)) LIMIT 1</code> </p> <p>In this case above, the user that I'm looking at is <code>user_id=2, he does have product_id=1 and product_id=2 in the database. And, in the user_products table, I do have a few records that list this user_id connected to each of these product_ids and associated with some variety_ids.</code> So it seems like I should be displaying some results on my show view, but I'm getting nothing. </p> <p>Finally, when I try the following:</p> <pre><code>&lt;% @user.products.each do |product| %&gt; &lt;%=h @user.user_products.find_by_product_id(product.id) %&gt; &lt;% end %&gt; </code></pre> <p>It displays the following in my views for each record: <code>#&lt;User_product:0x4211bb0&gt;</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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