Re: can this be done without using find_by_sql

tonypm
Sat, 14 Feb 2009 12:20:19 -0000

Fred,

Repair.find :all, :select => ''distinct repairs.*'', :joins
=> :notes, :conditions => ["flagged = ?", true]

nice - many thanks.  just needed to fix "notes.flagged"
--------------------------------------

In reality I have a generic notes model, so my usage is slightly more
complex:

In Repair I have:

has_many :repair_notes, :foreign_key=>''note_for''

has_many :flagged_repair_notes, :class_name=>"RepairNote",
:foreign_key=>''note_for'',
               :conditions=>{:flagged=>true}

Then:

Repair.find :all, :select => ''repairs.*'', :joins
=> :repair_notes, :conditions => ["notes.flagged = ?", true]

Creates lovely sql

SELECT repairs.* FROM `repairs` INNER JOIN `notes` ON notes.note_for repairs.id
AND (`notes`.`type` = ''RepairNote'' ) WHERE (notes.flagged 1)

I wonder if there is a  way to use the flagged_repair_notes in the
find for flagged repairs.

ps.
I have real admiration for the guys who do the ActiveRecord SQL
generation magic.

Thanks to all
Tonypm