Rails help looping trough has one and belongs to association
- by Rails beginner
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