[Rails] Re: can this be done without using find_by_sql

2009-02-14 Thread Frederick Cheung
On 14 Feb 2009, at 11:33, tonypm wrote: Hi, I am increasingly needing to do some more complex finds involving several table associations. I can usually find an SQL solution, but find it hard to think these out using ActiveRecord find techniques. I guess I am thinking in SQL terms, when

[Rails] Re: can this be done without using find_by_sql

2009-02-14 Thread Phlip
tonypm wrote: Repair has_many :notes SELECT * FROM repairs where exists (select * from notes where repairs.id=notes.repair_id and and notes.flagged) r = Repair.find(...) notes = r.notes.find_by_flagged(true) I can't think of a way to learn how deep the ActiveRecord DSL

[Rails] Re: can this be done without using find_by_sql

2009-02-14 Thread Phlip
Frederick Cheung wrote: Repair has_many :notes SELECT * FROM repairs where exists (select * from notes where repairs.id=notes.repair_id and and notes.flagged) I forgot my basic SQL! I think that's just SELECT * FROM repairs r, notes n WHERE r.id = n.repair_id AND

[Rails] Re: can this be done without using find_by_sql

2009-02-14 Thread tonypm
Just to finish the story. Sorry - the distinct in the select is needed, I missed it out. Going a step further, I now have: named_scope :flagged_repairs, :select = 'distinct repairs.*', :joins = :repair_notes, :conditions = [notes.flagged = ?, true] So in my search, where I am building a

[Rails] Re: can this be done without using find_by_sql

2009-02-14 Thread tonypm
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

[Rails] Re: can this be done without using find_by_sql

2009-02-14 Thread DarkTatka
named scope is definitely the way to go. check out http://guides.rubyonrails.org/active_record_querying.html#_named_scopes for more options (especialy named scope with argument) On 14 Ún, 13:32, tonypm tonypmar...@hotmail.com wrote: Just to finish the story. Sorry -  the distinct in the