Kleber S.
Kleber S.

Reputation: 8240

How to use correctly the associations

Im doing a model association between two models that are called Resume and Province.

Where:

class Resume < ActiveRecord::Base
has_one :province
end

and

class Province < ActiveRecord::Base
belongs_to :resume
end

at this point, everything is okay, but when I'm listing all resumes, I want to display the province name instead the province_id.

So, whats the better way to do such thing without have to perform a select for every single record?

Maybe this association is wrong.

In the Province table I have only the name and id fields.

province - id - name

resume - name - lastname - ... - province_id

Tell me if you need more details.

Upvotes: 0

Views: 120

Answers (1)

Markus Proske
Markus Proske

Reputation: 3366

Try something like this:

controller.rb

@resumes = Resume.all

in your view:

<% @resumes.each do |resume| %>
  <%= resume.province.name %>
<% end %>

Upvotes: 2

Related Questions