Best way to get data from n+1 models

Hello there.

Sometime ago I asked was the best way to retrieve data like an “union
all”, and unfortunely I could not find the URL on the forum. I asked
about, for example, hotel rooms and reservation. On another case, I
could ask for a plane occupation: a model that takes care of the sold
tickets and another one with the reservations. To get the full map of
the plane I need to get all the data from both models, on a specific
date and time. So if I have:

class Ticket extends ActiveRecord::Base
end

class Reservation extends ActiveRecord::Base
end

I could ask for tickets and reservations like

t = Ticket.find(:all,:conditions=>[“flight_id=? and
date=?”,flight,date])
r = Reservation.find(:all,:conditions=>[“flight_id=? and
date=?”,flight,date])

And get the full map with t.concat®. That was an answer for my
previous question and I thank you dudes for that.

My doubt now is what’s the best place to put this stuff. Suppose I want
a FlightMap object, where I can send the flight id and date and get the
array with the info above. The best way is create a model (with no
table) to do this, with a method to return the data? Like

class FlightMap
def map(flight_id,date)

end
end

FligthMap.map(flight_id,date)

Is that correct? The file flight_map.rb will be on the models directory,
creating the FlightMap model, used just to join information of the other
tables? There is no FligthMap table to join this kind of information, is
a dynamic operation based on a specific date, retrieving the data from
the other tables.

I think it works but need some more opinions if this is the correct way
to do that. :wink:

Thanks!

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