Rails help looping trough has one and belongs to association

Posted by Rails beginner on Stack Overflow See other posts from Stack Overflow or by Rails beginner
Published on 2011-03-04T23:15:40Z Indexed on 2011/03/04 23:25 UTC
Read the original article Hit count: 332

This is my kategori controller show action:

  def show
    @kategori = Kategori.find(params[:id])
    @konkurrancer = @kategori.konkurrancer

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @kategori }
    end
  end

This is kategori view show file:

<% @konkurrancer.each do |vind| %>
      <td><%= vind.name %></td>
      <td>4 ud af 5</td>
      <td><%= number_to_currency(vind.vaerdi, :unit => "DKK", :separator => ".", :delimiter => ".", :format => "%n %u", :precision => 0) %></td>
      <td>2 min</td>
      <td>Nyhedsbrev</td>
      <td><%= vind.udtraekkes.strftime("%d %B") %></td>
   </tr>
<% end %>

My kategori model:

class Kategori < ActiveRecord::Base
has_one :konkurrancer
end

My konkurrancer model:

class Konkurrancer < ActiveRecord::Base
belongs_to :kategori
end

I want show all of the konkurrancer that have an association to the kategori model

With my code I get the following error: NoMethodError in Kategoris#show

Showing C:/Rails/konkurranceportalen/app/views/kategoris/show.html.erb where line #12 raised:

undefined method `each' for "#":Konkurrancer

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails-3