As a developer, I need from time to time to quickly execute SQL, or check something in DB, truncate a table, etc when I'm in the rails console.
You may already know about using
ActiveRecord::Base.connection.execute("SQL query") solution, but I must admit I'm a lazy developer when it comes to doing routine tasks or things where I need to type long pieces of code. And actually a snippet of code above is not everything that you need to type, you may also need to add
.to_a to see the result of the query.
I know the following rule:
So, I tried to solve my issue as a developer and create an open-source solution to use it from project to project: https://github.com/igorkasyanchuk/execute_sql
I'll explain how it works.
Step of creation gem I'll skip, but basically you need to create a new gem (
rails plugin new <name>).
Now we need to understand how to add our own method just in the rails console.
module ExecuteSql class Railtie < ::Rails::Railtie console do TOPLEVEL_BINDING.eval('self').extend ExecuteSql::ConsoleMethods end end end
This is a piece of code from my gem. You can see that Rails provides a helper method
console where you can pass the code which will be executed when you starting
In our case this is
In case you have wondered, what this top-level constant TOPLEVEL_BINDING is all about better to check sample of usage below:
a = 42 p binding.local_variable_defined?(:a) # => true p TOPLEVEL_BINDING.local_variable_defined?(:a) # => true def example_method p binding.local_variable_defined?(:a) # => false p TOPLEVEL_BINDING.local_variable_defined?(:a) # => true end example_method
More details about
TOPLEVEL_BINDING you can find here.
 pry(main)> TOPLEVEL_BINDING.eval('self') => main  pry(main)> TOPLEVEL_BINDING => #<Binding:0x0000000001350b00>
As you can see TOPLEVEL_BINDING.eval('self') is basically the context of our console app.
So the last thing for us to do is to extend the
main app with our class methods from
ExecuteSql::ConsoleMethods module, source code is here.
Now my life is easier :)
And I hope you can implement your own utility methods.