Have Rails.logger Output Sent to the Console (Surprise: created_at Is Protected from Mass Assignment)

While working on a feature, I was wondering why my test was returning zero records when I’m quite sure the query was written properly (it was just a very simple query about a date range).

I suspected there was something wrong with the database query, so I looked for a way to redirect the logs that Rails produces into the console where I’m running RSpec (and Pry).

Surprisingly, it wasn’t that the query was wrong (it was just what I wanted). It was this:

WARN – : WARNING: Can’t mass-assign protected attributes for OrderLine: created_at

It does make your tests look messy, so I recommend using this configuration only when you want to know exactly what Rails is trying to do.

# config/environments/test.rb

config.logger = Logger.new(STDOUT)
config.logger.level = Logger::DEBUG

Note that this is for Rails 3. As the Stack Overflow answer by tommes explains, the configuration is slightly different for Rails 4. See more at http://stackoverflow.com/questions/11770552/how-to-get-rails-logger-printing-to-the-console-stdout-when-running-rspec

Comments

comments powered by Disqus