DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Extract Sql From Active Record

09.29.2009
| 3562 views |
  • submit to reddit
        // This method extends ActiveRecord and return the sql generated 
// For instance: City.view_sql(6) will return "select * from cities where id=6"   
// you can use all ActiveRecord stuff (includes, order, conditions....)

module ActiveRecord
  class Base
    class << self
      def view_sql(*args)
        options = args.extract_options!
        validate_find_options(options)
        set_readonly_option!(options)
        construct_finder_sql(options)   
      end
    end
  end
end