こんにちは。
同様の質問が、stackoverflow にあったので挙げておきます。
to_sql not working on update_attributes or .save
これの回答に
These methods both return a boolean. You can't invoke to_sql on a boolean.
とあるように、update
やcreate
は、booleanを返すので、to_sql
を使うことは
できませんが、上記の回答および回答にあるリンク先の別の質問
Log every SQL query to database in Rails
を見ると、以下のような特異メソッドをDBへのコネクションに追加すると、
(実際に動かして確認していませんが、)SQLをファイルに出力することを
実現できそうです。
ruby
1connection = ActiveRecord::Base.connection
2class << connection
3 alias :original_exec :execute
4 def execute(sql, *name)
5 # try to log sql command but ignore any errors that occur in this block
6 # we log before executing, in case the execution raises an error
7 begin
8 file = File.open(RAILS_ROOT + "/log/sql.txt",'a'){|f| f.puts Time.now.to_s+": "+sql}
9 rescue Exception => e
10 ;
11 end
12 # execute original statement
13 original_exec(sql, *name)
14 end
15end
以上参考になれば幸いです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/04 22:01
2018/01/04 22:10