I'm working on a car pooling application where users can add lifts and are able to select multiple stops for each lift ( A to B via c, d, e). Now when a user searches the database for a lift the results should also include lifts like 'A to d', 'c to B' or 'c to e' and so on.
I got this working with Rails 2.3.5 using the below code but struggle moving it over to Rails 3. I'm sure there must be a cleaner way to achieve what i'm trying to do and move some code to the model.
it would be great if someone could help me on this one.
class Lift < ActiveRecord::Base
has_many :stops
end
class Stop < ActiveRecord::Base
belongs_to :lift
belongs_to :city
end
class City < ActiveRecord::Base
has_one :stop
end
@lifts = Lift.find(
:select => "lifts.id, bs.city_id as start_city_id, bs2.city_id as destination_city_id",
:from => "lifts",
:joins => "LEFT JOIN stops bs ON lifts.id = bs.lift_id
LEFT JOIN stops bs2 ON lifts.id = bs2.lift_id
JOIN cities bc ON bs.city_id = bc.id
JOIN cities bc2 ON bs2.city_id = bc2.id",
:include => [:stops, :cities],
:conditions => "bs.lift_id = bs2.lift_id AND bs.position < bs2.position"
#uses the position attribute to determine the order of the stops
)