0

I have three relevant models: Vendor, Item, InventoryItem. I'm having difficulty understanding how to tap into associations to return associated attributes.

class Item < ActiveRecord::Base
has_many    :inventory_items
has_many    :vendors, through: :inventory_items
accepts_nested_attributes_for :inventory_items, :vendors

class InventoryItem < ActiveRecord::Base
belongs_to  :item
belongs_to  :vendor

class Vendor < ActiveRecord::Base
has_many    :inventory_items
has_many    :items, through: :inventory_items

I'm trying to return the vendors who sell an item, and the price they sell it for. Here's my SearchResults index view:

<table>  
<tr class="search-table">  
  <td>Product</td>
  <td>Details</td>
  <td>Brand</td>
  <td>Code</td>
  <td>Vendors</td>
  <td>Price</td>

</tr>

<% @items.each do |item| %>

 <tr class="search-table"> 
  <td><%= item.product %></td>
   <td><%= item.details %></td>
   <td><%= item.brand %></td>
   <td><%= item.code %></td>
   <td><%= #how to return vendors? %></td>
   <td><%= #how to return price? %></td>
 </tr>
<% end %>
</table>

Here is my SearchResultsController:

class SearchResultsController < ApplicationController

def index
    @search = Item.solr_search do
        fulltext params[:search]
        end

    @items = @search.results
   end
end

I'm newish at RoR so any input is welcome. Thanks in advance!

EDIT

Here is what is returned from rails console when given Item.first.vendors

Item Load (0.7ms) SELECT "items".* FROM "items" LIMIT 1 Vendor Load (0.9ms) SELECT "vendors".* FROM "vendors" INNER JOIN "inventory_items" ON "vendors"."id" = "inventory_items"."vendor_id" WHERE "inventory_items"."item_id" = 1 => []

SOLUTION EDIT I had some fundamental errors in my model associations that wouldn't allow me to utilize those relationships. I cleaned up those associations by getting rid of duplicate fields (in this case :item_id and :product_code) and the answer below worked perfectly.

settheline
  • 3,333
  • 8
  • 33
  • 65
  • Where does the price method/property live? Vendor? InventoryItem? – craig.kaminsky Aug 05 '13 at 22:05
  • Price is an attribute of InventoryItem. I'm wondering if my primary key setup is confusing active record. When I created the Item and InventoryItem tables I forgot to leave off their respective id's. These two tables should relate through the product code attribute, which is a column in both Item and InventoryItem tables. I don't believe it's a primary key in either though. Problem? – settheline Aug 06 '13 at 04:45
  • 1
    That could certainly lend itself to being an issue. You may need to declared the FK (foreign key) in the relevant model (not totally sure without seeing the full code/model relations). This SO question *may* help :) - http://stackoverflow.com/questions/16222838/has-many-through-with-a-foreign-key – craig.kaminsky Aug 07 '13 at 17:57
  • Very helpful craig, thank you. I cleaned up my model associations after looking at the SO question you linked to and got things working. – settheline Aug 07 '13 at 22:03

1 Answers1

2

In order to list vendors of a specific item, just replace :

<td><%= #how to return vendors? %></td>

With :

<% item.vendors.each do |vendor| %>
 <%= vendor.name %><br/> 
<% end %>
Zack Braksa
  • 1,628
  • 18
  • 26
  • Hmmm... that code runs okay, but doesn't return any vendor names in my view. I checked my db to ensure that there all of the necessary pieces are there to actually return something, so not sure what to do. Any ideas for a good console test to see if I can make things work in there? – settheline Jul 29 '13 at 22:25
  • In rails console, try something like : `Item.first.vendors` – Zack Braksa Aug 05 '13 at 02:57
  • Tried that, seems to return an empty array. I posted my output from that in my question. Any idea why that array might be empty? – settheline Aug 05 '13 at 21:52
  • 1
    Can you check your actual DB to ensure that the join table (i.e., inventory_items) has the correct entries for the item with id 1? – craig.kaminsky Aug 05 '13 at 22:04
  • @craig.kaminsky, yes I can, using a sqlit3 browser. Check out my answer to your other question, because I think you might be on to something related to my primary keys. – settheline Aug 06 '13 at 04:48