开发者

How to use correctly the associations

开发者 https://www.devze.com 2023-02-23 17:47 出处:网络
Im doing a model association between two models that are called Resume and Province. Where: class Resume < ActiveRecord::Base

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 wh开发者_如何学JAVAen 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.


Try something like this:

controller.rb

@resumes = Resume.all

in your view:

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

精彩评论

暂无评论...
验证码 换一张
取 消