Greyhounddad
Greyhounddad

Reputation: 115

Rails find data from association

I don't know if the title of the question is correct.

I have locations, hardware, and hardware_type tables but no nested routes.
I have set up the relationships so that location has_many hardwares and hardware belongs_to hardware_categories along with the reciprocal relationships.

In my locations controller in the show action I have

class LocationsController < ApplicationController
before_action :set_location, only: [:show, :edit, :update, :destroy]

# GET /locations
# GET /locations.json
def index
   @locations = Location.all
end

 # GET /locations/1
 # GET /locations/1.json
 def show
   @hardwares = Hardware.where(:location_id => params[:id])
 end

And in my show page

<% @hardwares.each do |hardware| %>
  <tr>
    <td><%= hardware.name %></td>
    <td><%= hardware.asset_tag %></td>
    <td><%= hardware.serial_number %></td>
    <td><%= hardware.note  %></td>
    <td><%= hardware.hardware_category_id  %></td>
  </tr>
<% end %>

This works and the hardware.hardware_category_id returns the correct number.

I would like to show the name of the hardware_category instead

I thought something like

<td><%= hardware.hardware_category_id.name  %></td>

would work but I get undefined method 'name' for 1:Fixnum
How would I get access to this data

schema

create_table "hardware_categories", force: true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "hardwares", force: true do |t|
  t.string   "name"
  t.string   "asset_tag"
  t.string   "serial_number"
  t.text     "description"
  t.text     "note"
  t.string   "cost"
  t.date     "purchase_date"
  t.date     "warranty_end_date"
  t.date     "eol"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "mdl"
  t.integer  "location_id"
  t.integer  "hardware_category_id"
  t.integer  "manufacturer_id"
  t.integer  "supplier_id"
end

Upvotes: 3

Views: 294

Answers (2)

KyleMac
KyleMac

Reputation: 1

hardware.hardware_category_id.name finds the hardware_category_id, then you find the name of that id.

In this case hardware.hardware_category_id == 1.

So you are calling 1.name and as 1 is a fixnum, and doesn't have the method name, you are getting the undefined method name for 1:Fixnum error.

You could create a new field in hardware called hardware_category_name and then call hardware.hardware_category_name.

Upvotes: 0

DickieBoy
DickieBoy

Reputation: 4956

<%= hardware.hardware_category.name %> Is what you need.

No need for the _id. The name of the association is as you defined it in the model. I assume its something like:

belongs_to :hardware_category

I recommend reading this guide, before you try doing much more rails. http://guides.rubyonrails.org/association_basics.html

The reason you are seeing that error, is because the hardware_category_id is exactly that, an Integer(FixNum) and the FixNum class doesn't have a method/attribute called name.

--

Rich Peck edit

A better way to do this will be to use .try to evaluate whether the corresponding hardware_category exists...

<%= hardware.hardware_category.try(:name) %>

... this will prevent any "not found" errors if the corresponding object is non existent.

Upvotes: 1

Related Questions