Sunday, September 23, 2012

[Rails] 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

--
Posted via http://www.ruby-forum.com/.

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate