On 10 May 2010 21:56, sso <[email protected]> wrote:
> I'm setting up a music catalogue to learn my way around RoR. In
> index.html.erb I would like to show the band name instead of the id.
> What is the proper way to do this?
>
> # records.rb

That should be record.rb, not records

> class Record < ActiveRecord::Base
>  belongs_to :band;
> end
>
> # bands.rb

Again, should be singular

> class Band < ActiveRecord::Base
>  has_many :records
> end
>
> # views/records/index.html.erb
> ...
> <% @records.each do |record| %>
>  <tr>
>    <td><%=h record.band.name %></td>

That should work, assuming you change the filenames above.  Watch out
for the case where no band has been allocated, however, ie record.band
is nil

Colin

>    <td><%=h record.title %></td>
>    <td><%=h record.description %></td>
> ...

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to