When last I tried to to this there was no official way to do this. 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 risk that Rails 3 will totally break it, but for debugging, it is an ok solution The method is construct_finder_sql(options) (lib/active_record/base.
Rb:1681) you will have to use send because it is private.
When last I tried to to this there was no official way to do this. 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 risk that Rails 3 will totally break it, but for debugging, it is an ok solution.
The method is construct_finder_sql(options) (lib/active_record/base. Rb:1681) you will have to use __send__ because it is private.
This is close to what I'm thinking. I guess a person could write a plugin that would do something like: SampleModel. Find(:all, :select => "DISTINCT(*)", :conditions => "date > #{self.
Date}", :limit => 1, :order => 'date', :group => "date"). Show_generated_sql and have this call the construct_finder_sql method. – rswolff Aug 28 '09 at 17:02 With DataMapper you could because it doesn't run the query right away.
ActiveRecord on the other hand executes the query immediately. Show_generated_sql will be acting on an already retrieved dataset from find. – John F.
Miller Aug 28 '09 at 20:55.
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, Logger. New(STDOUT) For Rails 3: ActiveRecord::Base.
Logger = Logger. New(STDOUT).
This isn't working for me in rails console. Does it work only for irb loaded directly from the shell? (or was it removed for rails 3?) – Eric Hu Mar 23 at 19:34 1 They moved it to a more sensible place for Rails 3... see my updated version.
– dasil003 Apr 7 at 3:08 Is there any way to do this automatically every time I start the console? Something like a before load hook? – stream7 Jul 6 at 7:20 1 @stream7..I don't know if you need this now, but you can move this code to environment.rb..if "irb" == $0;ActiveRecord::Base.
Logger = Logger. New(STDOUT);end..got this from comments in http://weblog.jamisbuck.org/2007/1/8/watching-activerecord-do-it-s-thing – rubyprince Oct 24 at 8:45.
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 = Logger. New(STDOUT) end That will output SQL statements into your irb session as you go. EDIT: Sorry that will execute the query still, but it's closest I know of.
1 for valiant effort – MattC Aug 27 '09 at 23:58 This is what I've been doing, but I'm more interested in simply seeing the projected SQL the ActiveRecord query will generate. I'm surprised there's no simple way to do this... – rswolff Aug 28 '09 at 16:59.
This is what I usually do to get SQL generated in console -> script/console Loading development environment (Rails 2.1.2) >> ActiveRecord::Base. Logger = Logger. New STDOUT >> Event.
First You have to do this when you first start the console, if you do this after you have typed some code, it doesn't seem to work Can't really take credit for this, found it long time ago from someone's blog and can't remember whose it is.
1 I'm pretty sure it was Jamis Buck's blog: weblog.jamisbuck. Org/2007/1/8/… – rswolff Aug 28 '09 at 17:03 I'm not sure if it's due to Rails 2.3 or something in my environment, but this doesn't work for me. See my response below.
– dasil003 Oct 16 '09 at 5:23.
Try the show_sql plugin. The plugin enables you to print the SQL without running it SampleModel. Sql(:select => "DISTINCT(*)", :conditions => "`date` > #{self.
Date}", :limit => 1, :order => '`date`', :group => "`date`").
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 ConnectionAdapters class AbstractAdapter def log_with_raise(sql, name, &block) puts sql raise 'aborting select' if caller. Any?
{ |l| l =~ /`select'/ } log_without_raise(sql, name, &block) end alias_method_chain :log, :raise end end end.
Stick a put 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.
For example: class Contact 10000') end Then, somewhere you can do: puts Contact. Frequently_contacted. To_sql.
In Rails 3 you can add this line to the config/environments/development. Rb config. Active_record.
Logger = Logger. New(STDOUT) It will however execute the query. But half got answered.
That will output SQL statements into your irb session as you go. EDIT: Sorry that will execute the query still, but it's closest I know of. EDIT: Now with arel, you can build up scopes/methods as long as the object returns ActiveRecord::Relation and call .
To_sql on it and it will out put the sql that is going to be executed.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.