rails 2.3将哈希转换为mysql查询

rails 2.3将哈希转换为mysql查询,mysql,ruby-on-rails,ruby,Mysql,Ruby On Rails,Ruby,我试图找出rails是如何转换散列的,例如这是一个示例,请不要从字面上理解这一点,因为我知道此查询与User相同。find1: { :select => "users.*", :conditions => "users.id = 1", :order => "username" } 进入: 从users.id=1的用户中选择users.*按用户名排序 我能找到的最接近的东西是ActiveRecord::Basefind\u every def fin

我试图找出rails是如何转换散列的,例如这是一个示例,请不要从字面上理解这一点,因为我知道此查询与User相同。find1:

{
    :select => "users.*",
    :conditions => "users.id = 1",
    :order => "username"
}
进入: 从users.id=1的用户中选择users.*按用户名排序

我能找到的最接近的东西是ActiveRecord::Basefind\u every

 def find_every(options)
   begin
     case from = options[:from]
     when Symbol
       instantiate_collection(get(from, options[:params]))
     when String
       path = "#{from}#{query_string(options[:params])}"
       instantiate_collection(format.decode(connection.get(path, headers).body) || [])
     else
       prefix_options, query_options = split_options(options[:params])
       path = collection_path(prefix_options, query_options)
       instantiate_collection( (format.decode(connection.get(path, headers).body) || []), prefix_options )
     end
   rescue ActiveResource::ResourceNotFound
     # Swallowing ResourceNotFound exceptions and return nil - as per
     # ActiveRecord.
     nil
   end
 end

我不确定如何修改它,只返回原始的mysql语句。

因此,经过几个小时的挖掘,我找到了一个答案,尽管它不是很好

class ActiveRecord::Base
  def self._get_finder_options options
    _get_construct_finder_sql(options)
  end

  private
  def self._get_construct_finder_sql(options)
    return (construct_finder_sql(options).inspect)
  end
end
将其添加为扩展将提供一个可公开访问的方法_get_finder_options,该方法返回原始sql语句

在我的例子中,这是对复杂查询的包装

SELECT COUNT(*) as count FROM (INSERT_QUERY) as count_table

这样我就可以把它和will_paginate宝石一起使用。这只在我当前的项目中进行过测试,因此如果您试图复制,请记住这一点。

为什么要在此处使用哈希?您的查询将只是Rails中的User.find1。顺便说一句,当结果是一个特定的项目时,使用订单是没有意义的。这是真的,但它不包括我的问题。我正在使用paginate,最近我不得不使用HAVING关键字。不幸的是,我的分组结果在select中指定,该结果将被will_paginate销毁。但是,如果我能够实现上述功能,我可以调用paginate_by_sql,它将包装查询并返回正确的计数结果。