How to Get the Last SQL Query Performed by Activerecord in Ruby on Rails

How do I get the last SQL query performed by ActiveRecord in Ruby on Rails?

AFAIK, there's no easy way to access the list of queries. Nonetheless you can easily get access to them creating a super simple logger.

If you open the class ActiveRecord::ConnectionAdapters::AbstractAdapter you'll see a method called log. This method is invoked on each query to log the statement. By default, it logs all the statements with the Rails logger.

You can do something like

ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do

attr_reader :last_query
alias_method_chain :log, :last_query

def log_with_last_query(sql, name, &block)
@last_query = [sql, name]
log_without_last_query(sql, name, &block)
end

end

Now you can get the query with

ActiveRecord::Base.connection.last_query # => ...

SQL query to get last record based on table relation

you can do this query (Result here)

with x as (
select row_number() over (partition by p.id order by b.created_at desc) as rn,b.id as id_box,p.id as id_paper
from boxes b join stones s on b.stone_id = s.id
join papers p on p.id = s.paper_id)
select x.id_box from x where rn = 1

How to get last N records with activerecord?


Updated Answer (2020)

You can get last N records simply by using last method:

Record.last(N)

Example:

User.last(5)

Returns 5 users in descending order by their id.

Deprecated (Old Answer)

An active record query like this I think would get you what you want ('Something' is the model name):

Something.find(:all, :order => "id desc", :limit => 5).reverse

edit: As noted in the comments, another way:

result = Something.find(:all, :order => "id desc", :limit => 5)

while !result.empty?
puts result.pop
end

How to show SQL queries run in the Rails console?


Rails 3+

Enter this line in the console:

ActiveRecord::Base.logger = Logger.new(STDOUT)

Rails 2

Enter this line in the console:

ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT)

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

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 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.

Edit: construct_finder_sql was removed in Rails 5.1.0.beta1.

How to get the last entered record field in rails with join and group by?

I think it's your group_by. That's a vanilla ruby enum. Have you tried .group('jobs.id')? group is an activerecord query helper that will run the sql group by clause

Retrieving sql queries from Active-record queries in Rails 3

You can debug ActiveRecord queries from a console.

Hit rails console and enter:

ActiveRecord::Base.logger = Logger.new(STDOUT)

Rails efficient method to query table from last record backward or reverse

I changed the logic to efficiently use "created_at ASC". It makes sense with my overall change in logic.



Related Topics



Leave a reply



Submit