Find by id in the view template dynamically

Rails 3.1.3

I have a model ‘Airline’, whose STRING column is ‘company’ only.
Also, another model ‘Plan’ has an INTEGER column ‘airline_id’.

I would like to show the ‘company’ name (string) in a template like

  <% @plans.each do |plan| %>
  Airline: <%= @airlines.find(plan.airline_id).first.company %><br

/>

<% end %>

in the ‘plans_controller.rb’,

  @plans = Plan.all
  @airlines = Airline.all

Obviously this does not work although no error appears.
It shows ‘Airline’ all the same, where each plan should have a distinct
airline company.
Certainly the database stores the airline company names which correspond
to the
plans. The loop in the view template gives, however, the identical
value.

Could anyone give some ideas that the template view shows distinct
airline company corresponding to each plan?

Thanks.

soichi

i recon your model relation should be like this:
plan belongs_to airline

if so you could do this on the view:
<% @plans.each do |plan| %>
Airline: <%= plan.airline.company %>
<% end %>

meanwhile in the plans_controller.rb:
@plans = Plan.all

hope this help

On Sun, Sep 23, 2012 at 4:25 PM, Soichi I. [email protected]
wrote:

  ...

airline company.
soichi
For more options, visit https://groups.google.com/groups/opt_out.


David A. Prasetya
RoR Developers

skype: david.angga
phone: +62 85 222 1 5555 2
*

Thanks for your answer. It worked.
I have totally forgotten the convenience association methods.
Rails is great!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs