I have two models : season and category. I want to display the category name in the season index view.
class Season < ActiveRecord::Base
attr_accessible :nb_down, :nb_up, :name, :category_id
has_one :category
accepts_nested_attributes_for :category
end
class Category < ActiveRecord::Base
attr_accessible :color, :name
belongs_to :season
end
In my season form, I add a category to season like this :
<%= f.collection_select :category_id, Category.all,:id,:name %>
On my season index view, I want to display all season columns AND category name. I'm displaying id cat doing :
<% @seasons.each do |season| %>
<tr>
<td><%= link_to season.id, season_path(season) %></td>
<td><%= season.name %></td>
<td><%= season.nb_up %></td>
<td><%= season.nb_down %></td>
<td><%= season.category_id %></td>
(...)
I want to display category name, and not id. I try to do "season.category.name" but no working. Where is my mistake please ?
Thanks :)
[EDIT] : i create a migration to add season_id in categories, and replaced I "has_one" by "belongs_to" in season class and it works : I can display the category name in the season index view.
create a migration
and add 'session_id' column in catogeries table
Run this command in project root directory in terminal
rails generate migration add_column_Session_id_to_categories session_id:integer
this will create migration, Now run this command
rake db:migrate
thats all you are done