0

Hello i created 2 tables (categories and employees),both are in a relationship and i want to show category_name(Categories table) in my employees index view instead of id's

****Here is my Categories Table******

  class CreateCategories < ActiveRecord::Migration
     def self.up
       create_table :categories do |t|
         t.string :category_name
       end
     end

     def self.down
       drop_table :categories
     end
  end

****Here is my Enployees Table******

  class CreateEmployees < ActiveRecord::Migration
    def self.up
      create_table :employees do |t|
         t.string  :name
         t.string  :last_name
         t.integer :categories_id 
      end
    execute "ALTER TABLE employees ADD CONSTRAINT fk_employees_categories FOREIGN KEY (categories_id) REFERENCES categories (id)"
    end

    def self.down
      drop_table :employees
    end
  end


****Here is my Employee controller******

class EmployeeController < ApplicationController
   def index
     @employees = Employee.all
   end
end

****Here is my Category controller******

class CategoryController < ApplicationController
   def index
     @categories = Category.all
   end
end

****Here is my Category Model******

class Category < ActiveRecord::Base
    has_many :employees
end

****Here is my Employee Model******

class Employee < ActiveRecord::Base
    belongs_to :category
end


****Here is Employee view*******

<table>
<tr>
    <th>Employee</th>
    <th>Last Name</th>
    <th>Category</th>
</tr>
    <% @employees.each do |e| %>
<tr>
    <td><%=h e.name %></td>
    <td><%=h e.last_name %></td>
    <td><%=h e.categories_id %>
</td>

Here in e.categories_id i want to show categories_name that is from my categories table

<td>
    <%= link_to ("View" ,:controller=>"employee",:action=>"show", :id=>e.id )  %>
</td>
<td>
    <%= link_to ("Edit", :controller=>"employee",:action=>"edit",:id=>e.id  ) %>
</td>
<td>  
    <%= link_to ( "Delete",:controller=>"employee",:action=>"destroy", :id=>e.id ,:confirm=>"sure?"  %>
</td>
</tr>
   <% end %>
</table>

Can someone help me with this problem please?

4 Answers4

0
<%= h e.category.category_name %>

will do the trick.

Note that if you just do this, it will cause an SQL query to be executed for each row that you're displaying (known as the N+1 problem).

So in your controller, you'll have to change

@employees = Employee.all

to

@employees = Employee.find(:all, :include => [:category])

This will force eager loading, and reduce the number of queries to 2.

(As a side note, you really should have moved on to Rails 3 by now. Rails 4 is current. Each version offers a lot more features than the last.)

  • I tried your code but i got this message. undefined method `includes' for # i'm working with Rails 2.3.5 and ruby 1.8.7 – Charlie Ramos Aug 22 '13 at 15:00
0

In your employee index views,

<td><%=employee.categories.category_name %>

instead of

<td><%=h e.categories_id %>
Htoo Myat Aung
  • 666
  • 1
  • 7
  • 16
0

In Your employee controller add this It means that get category_name to appear on your index view instead of ID.

@category= Category.all()
@category_list=[]
@category.each do |c|
@category_list << [c.category_name,c.id]
end

and then in your index views

<td><%=employee.categories.category_name %>
Htoo Myat Aung
  • 666
  • 1
  • 7
  • 16
0

I know this is a late answer, but for anyone who is looking for an answer, here is how I accomplished this,

I have a table realties that has a one to many association with features table. I used the railscast method for multiple checkbox selection, so for each realty it has certain features. I wanted to show the features of each realty in its row in the index view. I had in the show view, features are shown like this,

@realty.feature_ids

where @realty in the controller is

@realty = Realty.find(params[:id])

however, in the index view the realty.feature_ids only showed numbers only not the names.

To work this out here is what I did in the index view:

<% @realties.each do |realty| %>
  <td>
    <% realty.feature_ids.each do |feature| %>
       <%= Feature.find(feature).name %>
    <% end %>
  </td>
<% end %>

so for the above question, loop through the ids with each loop, this will give you the id number, find it in the Category model with the .name method. So I guess the solution for you would look like this:

<% @employees.each do |employee| %>
  <td>
    <% employee.category_ids.each do |category| %>
       <%= Category.find(category).name %>
    <% end %>
  </td>
<% end %>

I hope this will benefit someone.