我正在使用PG exec_params进行原始sql查询:
var img = document.getElementsByClassName("img"),
i;
function imgclick(e) {
e.preventDefault();
e.stopPropagation();
var id=this.id; //e.target.id
}
for (i = 0; i < img.length; i++) {
img[i].addEventListener("click", imgclick);
}
执行此查询时,不会发生友好的SQL日志记录。有没有一种首选方法可以实现这一点,所以我实际上可以看到执行的内容,类似于我通过活动记录api进行常规查询的时间?
答案 0 :(得分:0)
我尝试过
create_table :tasks do |t|
t.boolean :a, default: false
t.boolean :b, default: false
t.boolean :c, default: false
t.string :d
t.string :e
end
class Task < ActiveRecord::Base
def self.test_sql
random_bool = [true, false]
sql = "insert into tasks (a, b, c, d, e) values ('0', '0', '0', ''1', 'I just dont know')"
row_count = ActiveRecord::Base.connection.raw_connection.exec_params(sql)
end
end
Task.test_sql # => #<PG::Result:0x00007faeef4461c8 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=1>
Task.last # => #<Task id: 1, a: false, b: false, c: false, d: "1", e: "I just dont know">
# here is the logs
# >> D, [2020-03-04T11:21:12.977892 #17467] DEBUG -- : ActiveRecord::InternalMetadata Load (0.2ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", "environment"], ["LIMIT", 1]]
# >> D, [2020-03-04T11:21:12.981050 #17467] DEBUG -- : (0.1ms) BEGIN
# >> D, [2020-03-04T11:21:12.981931 #17467] DEBUG -- : ActiveRecord::InternalMetadata Create (0.3ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "key" [["key", "environment"], ["value", "default_env"], ["created_at", "2020-03-04 10:21:12.981267"], ["updated_at", "2020-03-04 10:21:12.981267"]]
# >> D, [2020-03-04T11:21:12.982333 #17467] DEBUG -- : (0.3ms) COMMIT
# >> W, [2020-03-04T11:21:12.985424 #17467] WARN -- : Creating scope :system. Overwriting existing method TaskStatus.system.
# >> D, [2020-03-04T11:21:12.991951 #17467] DEBUG -- : Task Load (0.1ms) SELECT "tasks".* FROM "tasks" ORDER BY "tasks"."id" DESC LIMIT $1 [["LIMIT", 1]]