Re: [Rails] Find by id in the view template dynamically
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 Ishida <lists@ruby-forum.com> wrote:
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.
--
David Angga Prasetya
RoR Developers
skype: david.angga
phone: +62 85 222 1 5555 2
--
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