ruby on rails - Add model atribute to result of a query -


i have 3 models , (simplified):

class airline < activerecord::base   attr_accessible :name   has_many :airplanes   has_many :airplane_switches end  class airplane < activerecord::base   attr_accessible :airline_id, :register   belongs_to :airline   has_many :airplane_switches end  class airplaneswitch < activerecord::base   attr_accessible :airline_id, :airplane_id   belongs_to :airplane   belongs_to :airline end 

airplanes have been in airlines, needed model indicates if airplane in 1 or more airlines.

i building form let users upload info airplane, select airplane register (callsign) , list choose in airline was.

this work on ajax request. but, trying figure out how show airline name controller, avoid ajax call fetching json file name of airline based on airline_id in airplaneswitch.

@airplane = airplane.find_by_register(params[:register]) @airplane_switches = @airplane.airplane_switches # here need join each airline.name 

i think way more efficient, have no idea if it's possible do.

this should work:

@airplane.airplane_switches.select('*, airlines.name airline_name').joins(:airline) 

let's have variable airplane_switch contains airlineswitch instance fetched in way. need airline name is:

airplane_switch.airline_name 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -