Trishj
Trishj

Reputation: 17

How to access one model from a different view

I have two models named tasks and category. I need to access category table data from tasks view

views/tasks/_task.html.erb

<tbody>
  <tr>
    <td><%= task.description %></td>
    <td><%= task.category_id %></td>
    <td><%= task.status %></td>
  </tr> 
</tbody>

each category_id has corresponding name stored in Category table. So instead of displaying category_id (eg: 1,2,3..etc), I need to pull the corresponding name from Category table. (id:name ==> 1: Todo, 2: shopping..etc)

Upvotes: 0

Views: 27

Answers (1)

MZaragoza
MZaragoza

Reputation: 10111

if you have your model set the right way something like this

class Task < ActiveRecord::Base
  belongs_to :category
  ...
end

then in your view you should be able to say

<td><%= task.category.name %></td>

I hope I got the question right and also the answer

Happy hacking

Upvotes: 2

Related Questions