-1

Railsが次のようなハッシュを変換する方法を見つけようとしています(これは例です。これを文字通りに解釈しないでください。このクエリがUser.find(1)と同じであることを知っているため、概念を理解するために何かを組み合わせました):

{
    :select => "users.*",
    :conditions => "users.id = 1",
    :order => "username"
}

Into: SELECT users.* FROM users where users.id = 1 ORDER BY username

私が見つけることができる最も近いものは ActiveRecord::Base#find_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ステートメントが何であるかを返すようにこれを変更する方法がわかりません。

4

1 に答える 1

0

それで、数時間掘り下げた後、私は答えを思いつきましたが、それは素晴らしいことではありません.

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 gem でこれを引き続き使用できるようにします。これは現在のプロジェクトでのみテストされているため、複製しようとしている場合は、そのことを覚えておいてください.

于 2013-09-23T20:04:17.623 に答える