How can I get SQL statement created by ActiveRecord#find without actually executing it?

前端 未结 5 1706
灰色年华
灰色年华 2020-12-30 21:46

I am using will_paginate with some complicated queries and it is unable to correctly calculate number of total records (in order to display proper number of pag

5条回答
  •  孤城傲影
    2020-12-30 22:31

    For Rails 3:

    Check out the ActiveRecord::Relation docs at the Rails 3 docs.

    # get the relation
    rel = User.complex_scope.chained_complex_scope
    
    # get the SQL
    # this does not execute the query
    sql = rel.to_sql
    
    # find out how many records
    # this executes the query behind the scenes
    count = rel.size
    

提交回复
热议问题