Reputation:
In my first Ruby on Rails app I've a one-to-many association
class Battle < ActiveRecord::Base
has_many :rivals, :dependent => :destroy
accepts_nested_attributes_for :rivals, :allow_destroy => true
attr_accessible :question, :rivals_attributes
end
class Rival < ActiveRecord::Base
belongs_to :battle
has_attached_file :rival_image, :styles => { :normal => "300x300>", :thumb => "100x100>" }
end
Let's say a battle has 2 rivals
<% for rival in @battle.rivals %> <%= rival.name %> <% end %>
displays both rivals that belongs to battle
How do I display first rival and the second one where I need it?
Upvotes: 0
Views: 325
Reputation: 7612
If you want to access individual records from a association use:
<%= battle.rivals[0].name %>
Upvotes: 1