Do something like this, but not so hackish, and that takes into
account associations and joins...
config/environment.rb:
class ActiveRecord::Base
def self.return_sql(options)
construct_finder_sql(options)
end
end
Then...
$ ./script/runner "puts Occupation.return_sql(:conditions => 'id IN
(1,2,3)', :order => 'title')"
SELECT * FROM "occupations" WHERE (id IN (1,2,3)) ORDER BY title
Take a look at rails/activerecord/lib/active_record/base.rb line 1427
"def find_every" and just follow it through. Also line 558 "def
construct_finder_sql(options)" is relevant.
On Sep 17, 2008, at 4:13 PM, postscript07 wrote:
>
> Phillip, I don't think this is quite the same thing... am I right that
> in your function I still have to provide the sql statement myself?
> Rather, I am looking for a way of getting the sql generated by an
> ActiveRecord statement..
>
> On Sep 17, 11:45 pm, Phillip Koebbe <[EMAIL PROTECTED]>
> wrote:
>> A while ago, I had a similar need. I wanted to get the sql back but
>> wanted it to execute as well. I came up with this:
>>
>> # get the query back from ActiveRecord
>> class ActiveRecord::Base
>> def self.find_by_and_return_sql(sql)
>> sanitized_sql = sanitize_sql(sql)
>> return find_by_sql(sanitized_sql), sanitized_sql
>> end
>> end
>>
>> This is just for find_by_sql, but you might be able to adapt it for
>> the
>> other finders.
>>
>> Just put it in a file that gets loaded. I have a
>> custom_extensions.rb in
>> lib that I use for stuff like this.
>>
>> Peace.
>> --
>> Posted viahttp://www.ruby-forum.com/.
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---