Advanced find in Rails

Posted by jriff on Stack Overflow See other posts from Stack Overflow or by jriff
Published on 2010-04-05T15:45:57Z Indexed on 2010/04/05 16:13 UTC
Read the original article Hit count: 189

Filed under:
|

Hi all

I really suck at Rails' finders besides the most obvious. I always resort to SQL when things get more advanced than

Model.find(:all, :conditions => ['field>? and field<? and id in (select id from table)', 1,2])

I have this method:

def self.get_first_validation_answer(id)
a=find_by_sql("
  select answers.*, answers_registrations.answer_text
  from answers_registrations left join answers on answers_registrations.answer_id=answers.id
  where 
    (answers_registrations.question_id in (select id from questions where validation_question=true)) 
    and
    (sale_registration_id=#{id})
  limit 1
").first

a.answer_text || a.text if a
end

Can someone create a find method that gets me what I want?

Regards,

Jacob

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord