Hi --
On Tue, 8 Aug 2006, Eustáquio Rangel wrote:
> 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(r). 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)
> <code above inserted here>
> 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. ;-)
Assuming you have a Flight class and flights table, you could just do
something like:
class Flight < AR::Base
has_many :tickets
has_many :reservations
def map(date)
tickets.concat(reservations)
end
end
I''m not sure this is the optimal way to model the domain, but I think
it corresponds to what you''re describing.
David
--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.