How to modify complex sql query w/ join into rails 3?

I thought about that too but I don't have any idea on how to deal with the joins properly. Maybe you can give me some advise. – Ryan Foster Oct 8 '10 at 15:05.

I'm a little confused that you're using find's :select and :include options together. I know for a fact that :select is ignored when you're using :include (think there was a plugin to fix that, though). Also, you're including :cities, a relationship that doesn't appear in your code.

Anyway, if you're wanting to add some scopes to your Lift model, I'd consider switching to the Arel API, which is the preferred method in Rails 3. m.onkey.org/2010/1/22/active-record-quer... You could stuff that whole query into one scope (don't think you need the :from option - could be wrong): class Lift ... scope :with_all_stops, select('lifts. Id, bs.

City_id as start_city_id, bs2. City_id as destination_city_id'). \ 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'). \ includes(:stops, :cities). \ where('bs.

Lift_id = bs2. Lift_id AND bs. Position Or you could chain on your own conditions: Lift.

With_all_stops. Where('cities.Name="Topeka"'). It's really powerful.

If there are parts of that query that are useful on their own (doubtful in this particular case), you could break them out into their own scopes, then chain them all together when you call. Or chain them together in another scope, and just call it. Like I said, Arel can be really powerful.

Scope :with_all_stops, select('lifts.id, bs. City_id as start_city_id, bs2. City_id as destination_city_id').

Joins('LEFT JOIN stops bs ON lifts.id = bs. 'LEFT JOIN stops bs2 ON lifts.id = bs2. 'JOIN cities bc ON bs.

'JOIN cities bc2 ON bs2. City_id = bc2.id'). Lift_id = bs2.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions