Have you ever found yourself overwhelmed by the seemingly chaotic output of SQL queries in your Rails console or logs? Fear not! Introducing PpSql – the gem that transforms your SQL code into a neat and readable format. In this blog, we’ll guide you through the setup and usage of PpSql to elevate your SQL output, making it user-friendly and visually clearer.
Why PpSql?
PpSql replaces the standard ActiveRecord#to_sql method with a formatting gem, ensuring that your SQL queries are not just functional but also presentable. You’ll find decorated outputs in both the console and the Rails logs.
Getting Started with PpSql
- Prerequisites:
- Ruby 2.4+
- Rails 5.0+ (optional – will be injected automatically)
- For legacy versions, you can use
~ 0.2gem with Ruby 2.2, 2.3 or Rails 4.0, 4.1
Installation
To install PpSql, follow these simple steps:
# Add to your Gemfile
gem 'pp_sql', group: :development
# Execute the following command
bundle
Usage
Now that PpSql is installed, you’re ready to see it in action. The usage is straightforward:
Post.first.to_sql
For a custom string output, you can create a class:
class MyAwesomeDecoratedString < String
include PpSql::ToSqlBeautify
end
Integration with Rails
If you prefer not to overwrite the default #to_sql method, you can adjust this in your initializer:
PpSql.rewrite_to_sql_method = false
Additionally, to disable log formatting, use:
PpSql.add_rails_logger_formatting = false
Enhancing Your Application Record
A handy trick is to include the formatting within your ApplicationRecord:
class ApplicationRecord < ActiveRecord::Base
include PpSql::ToSqlBeautify if defined?(Rails::Console)
self.abstract_class = true
end
Troubleshooting
If you encounter any issues while using PpSql, here are some troubleshooting tips:
- Ensure your Ruby and Rails versions meet the gem's requirements.
- Check your Gemfile for proper gem listing and syntax.
- Run
bundle installto ensure all dependencies are satisfied. - If you see unexpected behavior related to formatting, verify changes in your initializer settings.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
With PpSql, you can start enjoying a more delightful interaction with your SQL queries in Rails. Formatting SQL outputs not only lends clarity but also aids in debugging and readability.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

