Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert rails activerecord to raw sql

In php codeigniter FW, I can do it easily by using get_compiled_select() method.

Is there any method that I can convert the query build by active record to raw sql. Ex:

User.find_by_name("abc")

should return raw query script something like

select * from user where name = "abc"
like image 588
user3783161 Avatar asked Nov 05 '15 10:11

user3783161


1 Answers

You can use to_sql (it gives you a sql query string, and can be called on any ActiveRecord association):

User.find_by_name("abc").to_sql

http://apidock.com/rails/ActiveRecord/Relation/to_sql

like image 118
Yury Lebedev Avatar answered Sep 27 '22 19:09

Yury Lebedev