Question

I havle belongs_to association model 'Timetableap' with model 'Aircompany'

     class Timetableap < ActiveRecord::Base
        belongs_to :aircompany
      ...
     end


  class Aircompany < ActiveRecord::Base
     has_many :timetableaps
     ...
   end

In 'Timetableaps' controller:

  class TimetableapsController < ApplicationController
   ...
   def index
     @t = Timetableap.search(params[:search_ap],params[:search_al])
   ...
   end

My structure tables "aircompanies" table

  create_table "aircompanies", :force => true do |t|
     t.string   "iata_code",        :null => false
     t.string   "icao_code",        :null => false
     t.string   "awb_prefix"
     t.string   "airline_name",     :null => false
     t.integer  "airport_id",       :null => false
     t.string   "country"
     t.string   "ap_hubs"
     t.date     "start"
     t.date     "end"
     t.datetime "created_at",       :null => false
     t.datetime "updated_at",       :null => false
   end

"timetableaps" table

  create_table "timetableaps", :force => true do |t|
     t.integer  "Flight_Number"
     t.integer  "aircompany_id"
     t.integer  "way_start"
     t.integer  "way_end"
     t.string   "TermStart"
     t.string   "GateStart"
     t.string   "TermEnd"
     t.string   "GateEnd"
     t.string   "TypeOfPlane"
     t.time     "TimeStart"
     t.time     "TimeEnd"
     t.date     "DateOfStartNav"
     t.date     "DateOfEndNav"
     t.integer  "s1"
     t.integer  "s2"
     t.integer  "s3"
     t.integer  "s4"
     t.integer  "s5"
     t.integer  "s6"
     t.integer  "s0"
     t.integer  "e1"
     t.integer  "e2"
     t.integer  "e3"
     t.integer  "e4"
     t.integer  "e5"
     t.integer  "e6"
     t.integer  "e0"
     t.datetime "created_at",     :null => false
     t.datetime "updated_at",     :null => false
   end

How can I display drop-down list in views with 'aircompany.airline_name'?

 <%= select_tag "search_al", options_from_collection_for_select(@t.group("aircompany_id"),"aircompany_id", **"aircompany"**), prompt: "All Airlines" %>
Was it helpful?

Solution

It's not clear if you want a drop down with all airlines or if they are supposed to be filtered in some way.

If you want all airlines, this works:

<%= select_tag "search_al", options_from_collection_for_select(Aircompany.all, :id, :airline_name), prompt: "All Airlines" %>

Since @t appears to be a collection of Timetableap records, you could chose to only display airlines found in the current collection of timetableaps:

<%= select_tag "search_al", options_from_collection_for_select(@t.map(&:aircompany).uniq, :id, :airline_name), prompt: "All Airlines" %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top