Rails activerecord where to sql

Viewed 1541

How to get sql for only condition?

Lead.where(status: 1).to_sql

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

I need only the condition to sql. ie, leads.`status` = 1

Also i have default scope. From that i need only that scope to sql

2 Answers

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.

Related