简体   繁体   English

Rails activerecord 到哪里去 sql

[英]Rails activerecord where to sql

How to get sql for only condition?如何仅获取条件的 sql?

Lead.where(status: 1).to_sql

SELECT `leads`.* FROM `leads` WHERE `leads`.`status` = 1

I need only the condition to sql.我只需要sql的条件。 ie, leads.`status` = 1即, leads.`status` = 1

Also i have default scope.我也有默认范围。 From that i need only that scope to sql从那以后我只需要那个范围到 sql

You can use Arel::SelectManager#where_sql :您可以使用Arel::SelectManager#where_sql

>> Lead.where(status: 1).arel.where_sql
=> "WHERE \"leads\".\"status\" = $1"

The following should work:以下应该工作:

Lead.where(status: 1).values[:where].ast.to_sql

We are in the internals of ActiveRecord here, so it comes without guarantee of course.我们在这里处于 ActiveRecord 的内部,所以当然不能保证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM