ActiveRecord记录SQL格式是指在使用ActiveRecord模式查询数据库时,生成的SQL语句的格式。下面是一种解决方法,其中包含了代码示例:
users = User.where(age: 18).order(name: :asc).limit(10)
puts users.to_sql
输出结果:
SELECT "users".* FROM "users" WHERE "users"."age" = 18 ORDER BY "users"."name" ASC LIMIT 10
users.each do |user|
puts "#{user.name}, #{user.age}"
end
输出结果:
John Doe, 18
Jane Smith, 18
...
通过以上步骤,可以使用ActiveRecord记录SQL格式,并且输出查询结果。
上一篇:ActiveRecord::ConnectionNotEstablished: ActiveRecord::Base没有与Mongoid建立连接池。
下一篇:ActiveRecord在Rails 4.2.8中不再在数组中找到重复的ID并保存到has-and-belongs-to-many关系中。