-2

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.

John
  • 198
  • 2
  • 10
  • 1
    Maybe the problem is that you write `saison` instead of `season`? – Mike Szyndel Jul 02 '13 at 13:53
  • No it's season (sorry for mistake), i edit it sorry – John Jul 02 '13 at 13:57
  • @John what is your error when you try to do `<%= season.category.name %>` – Deej Jul 02 '13 at 13:58
  • When i try season.category.name, there is this error : SQLite3::SQLException: no such column: categories.season_id: SELECT "categories".* FROM "categories" WHERE "categories"."season_id" = 1 LIMIT 1 – John Jul 02 '13 at 13:59
  • Well this indicates that there is no column `categories.season_id` in your `categories` table. I believe what you need to do is add `season_id` to your `categories` table. Also are you sure that you associations are set up correctly? Can a `season` not have many `categories`? – Deej Jul 02 '13 at 14:03
  • A season have only one category, but a category can be associate to many seasons. So you think i have to add a season_id to category ? – John Jul 02 '13 at 14:06

1 Answers1

0

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

Taimoor Changaiz
  • 10,250
  • 4
  • 49
  • 53
  • Thanx for your answer, i replace session by season, but i have a new error when i try to display season.category.name "undefined method `name' for nil:NilClass" – John Jul 02 '13 at 14:21
  • Great answer though I did comment telling @John that's what he needed to do. You need the points more than I do! – Deej Jul 02 '13 at 14:22
  • @John just replace this line <%= season.category.name if season.category.present? %> – Taimoor Changaiz Jul 02 '13 at 14:32
  • I replace "has_one" by "belongs_to" in season class and it works ! – John Jul 02 '13 at 15:09
  • @John I think season has one category not category has one season. So whats your purpose?? – Taimoor Changaiz Jul 02 '13 at 15:25
  • When I list all my seasons, I want to display a column that indicates the category of this season. The result is good with belongs_to on both models. – John Jul 02 '13 at 15:47
  • nope you are doing it wrong. Look first make it clear that wehter category belongs to season or season belongs to category. For instance, an account belongs to person, not person belongs to account. Hope you got something clear. for more help look at this http://guides.rubyonrails.org/association_basics.html – Taimoor Changaiz Jul 03 '13 at 05:49