How to Debug in Rails
View Helpers
debug
# some view
# prints out the objects in the view
<%= debug @quotes %>
# Prints out the following:
---
- !ruby/object:Quote
concise_attributes:
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: id
value_before_type_cast: 6
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: client_id
value_before_type_cast: 1
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: project_name
value_before_type_cast: Rempel, Kassulke and Waelchi-2
- !ruby/object:ActiveModel::Attribute::FromDatabase
to_yaml
# some view
# prints out the objects in the view
<%= simple_format @quotes.to_yaml %>
# Prints out the following:
---
- !ruby/object:Quote
concise_attributes:
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: id
value_before_type_cast: 6
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: client_id
value_before_type_cast: 1
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: project_name
value_before_type_cast: Rempel, Kassulke and Waelchi-2
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: project_description
inspect
<%= @quotes.inspect %>
# returns the following
#<ActiveRecord::Relation [#<Quote id: 6, client_id: 1, project_name: "Rempel, Kassulke and Waelchi-2", project_description: "Facilis
Logging
If you’re in a controller, you can debug via logging:
# some controller
logger.debug "All quotes: #{@quotes.inspect}"
Which produces this in the logs:
All quotes: #<ActiveRecord::Relation [#<Quote id: 6, client_id: 1, project_name: "Rempel, Kassulke and Waelchi-2", project_description: "Facilis expedita laudantium dolore nostrum pariatu...", currency: nil, exclusions: "", deliverables: nil, payment_terms: "30 days on IFA, IFC to be submitted after all paym...", created_at: "2023-11-30 04:56:33.310262000 +0000", updated_at: "2024-12-16 00:57:56.961092000 +0000", private_details: "", aasm_state: "won", quote_type: "material_take_off", variation_rate: 0.700809284126875e2, line_items_count: 2, bonus_percentage: 0.6e-1, token: [FILTERED], organisation_id: 1, live_link: "", design_review_rate: 0.4180055192682217e2>]>
Verbose logs
Verbose query logs are enabled by default in the development environment logs after Rails 5.2.
# development.rb
config.active_record.verbose_query_logs = true
Tagged logging. This could be useful when filtering out logs from different request_ids or subdomains or users:
logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
logger.tagged("BCX") { logger.info "Stuff" } # Logs "[BCX] Stuff"
Console
# some controller action
def show
console # show a debug console in the web browser.
# etc
end
Debug Gem
This requires a post in and of itself. There is no substitute to spending a few hours to read through the documentation, to experiment and become familiar with this tool.
Some notable commands:
rdbg -c -- rails server
# ensure "debug" is an installed gem in your Gemfile
# if using bundler
rdbg -c -- bundle exec ruby foo.rb
Advanced Debug
- https://dev.to/st0012/optimize-your-debugging-process-with-object-oriented-tracing-and-tappingdevice-39c6
Sources
Written on December 28, 2024