Quantcast
Channel: How can I see the SQL that will be generated by a given ActiveRecord query in Ruby on Rails - Stack Overflow
Browsing all 13 articles
Browse latest View live

Answer by Syamlal for How can I see the SQL that will be generated by a given...

You can simply use to_sql() function with the active recordForm.where(status:"Active").to_sql

View Article



Answer by Itai Sagi for How can I see the SQL that will be generated by a...

just use to_sql method and it'll output the sql query that will be run. it works on an active record relation.irb(main):033:0> User.limit(10).where(:username => 'banana').to_sql=> "SELECT...

View Article

Answer by rogerdpack for How can I see the SQL that will be generated by a...

My typical way to see what sql it uses is to introduce a "bug" in the sql, then you'll get an error messages spit out to the normal logger (and web screen) that has the sql in question. No need to find...

View Article

Answer by Tarang for How can I see the SQL that will be generated by a given...

This may be an old question but I use:SampleModel.find(:all, :select => "DISTINCT(*)", :conditions => ["`date` > #{self.date}"], :limit=> 1, :order => '`date`', :group => "`date`"...

View Article

Answer by Timbinous for How can I see the SQL that will be generated by a...

In Rails 3 you can add this line to the config/environments/development.rbconfig.active_record.logger = Logger.new(STDOUT)It will however execute the query. But half got answered :

View Article


Answer by pathdependent for How can I see the SQL that will be generated by a...

Stick a puts query_object.class somewhere to see what type of object your working with, then lookup the docs. For example, in Rails 3.0, scopes use ActiveRecord::Relation which has a #to_sql method....

View Article

Answer by Harish Shetty for How can I see the SQL that will be generated by a...

Try the show_sql plugin. The plugin enables you to print the SQL without running itSampleModel.sql(:select => "DISTINCT(*)", :conditions => ["`date` > #{self.date}"], :limit => 1, :order...

View Article

Answer by gtd for How can I see the SQL that will be generated by a given...

Similar to penger's, but works anytime in the console even after classes have been loaded and the logger has been cached:For Rails 2:ActiveRecord::Base.connection.instance_variable_set :@logger,...

View Article


Answer by penger for How can I see the SQL that will be generated by a given...

This is what I usually do to get SQL generated in console-> script/consoleLoading development environment (Rails 2.1.2)>> ActiveRecord::Base.logger = Logger.new STDOUT>> Event.firstYou...

View Article


Answer by also for How can I see the SQL that will be generated by a given...

You could change the connection's log method to raise an exception, preventing the query from being run.It's a total hack, but it seems to work for me (Rails 2.2.2, MySQL):module ActiveRecord module...

View Article

Answer by John F. Miller for How can I see the SQL that will be generated by...

When last I tried to do this there was no official way to do it. I resorted to using the function that find and its friends use to generate their queries directly. It is private API so there is a huge...

View Article

Answer by nitecoder for How can I see the SQL that will be generated by a...

Create a .irbrc file in your home directory and paste this in:if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER') require 'logger' RAILS_DEFAULT_LOGGER =...

View Article

How can I see the SQL that will be generated by a given ActiveRecord query in...

I would like to see the SQL statement that a given ActiveRecord Query will generate. I recognize I can get this information from the log after the query has been issued, but I'm wondering if there is a...

View Article

Browsing all 13 articles
Browse latest View live




Latest Images