turning off ActiveRecord logging in the console

It’s sometimes quite annoying to have SQL queries clogging up the Rails console when you don’t need to see it. You Can turn it off by setting ActiveRecord’s logger to nil

old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil

You can of course turn it back on again.

ActiveRecord::Base.logger = old_logger

since upgrading to macOS Sierra

Of course I need to re-install Command Line Developer Tools without XCode, same after every upgrade.

Robs-MBP:themadkatter rl$ git pull orgin master
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Robs-MBP:themadkatter rl$ xcode-select --install
xcode-select: note: install requested for command line developer tools

Itty Bitty City

Found this rather nice Ardunio kit, which integrates nicely with LEGO compatible components.


Hooking into ActiveRecord logging

Quick and dirty hook to log long SQL queries and their caller.

ActiveSupport::Notifications.subscribe 'sql.active_record' do |name, start, finish, id, payload|
  time = (finish - start) * 1000
  if payload[:sql] && time > 500
    time = (time * 1000).to_i / 1000.0
    c = caller.select { |l| l =~ /something_(core|music|artists)/ }[0]
    logger = Logger.new(Rails.root.join('log', 'active_record_slow_queries.log'))
    logger.info("(#{time}s) #{payload[:sql]} - #{c}")

Ruby WTF #1

Answers on a postcard.

2.2.0 :004 > %w( dave )
=> ["dave"] 
2.2.0 :005 > %w) dave )
=> ["dave"]

I'm so dizzy

Playing with generating SVGs manually, we’ll with a txt file of the image and HAML and Ruby but still…

It’s only appropriate really to link to YolkFolk at this point.

Comparing plain old Ruby objects

Try Google-ing for <=> it not very helpful. It’s the spaceship method, I can never remember that. So in my case I had a custom classes representing a remote resource and I needed to compare if these objects were similar based on their id. If you compare object == other_object, they won’t be the same because they are different objects in memory. You could write…

class Tentacle
  def ==(other)
    self.id == other.id

..but what about >, >=, <, <=, !=, etc. Comparable covers all of this.

class Tentacle
  include Comparable
  def <=>(other)
    self.id <=> other.id

…but what if the object you’re comparing against is an instance of another class?

class Tentacle
  def <=>(other)
    return nil unless other.is_a?(self.class)
    self.id <=> other.id

And that was the day that was.

Test expiry of a PEM Certificate

Want to test the expiry of PEM Certificate?

Robs-iMac:something roblacey$ openssl x509 -enddate -noout -in config/certificates/ios/production.pem
notAfter=Apr 21 16:55:18 2017 GMT


RangeError (2918996727 is out of range for ActiveModel::Type::Integer with limit 4):

If you’re attempting to store an IP as an Integer in MySQL, you’ll find that an Integer just isn’t enough. I hadn’t noticed for a few days and came home to a log file full of

RangeError (2918996727 is out of range for ActiveModel::Type::Integer with limit 4):

Easily fixed.

Database changed
MariaDB [robl]> ALTER TABLE fossa_logs MODIFY ip bigint(20) NULL;
Query OK, 209 rows affected (0.03 sec)             
Records: 209  Duplicates: 0  Warnings: 0

Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT

Comparison of a UTF-8 string and an ASCII-8BIT string fails.

2.2.2 :031 > "∞".force_encoding('UTF-8').include?("∞".force_encoding('ASCII-8BIT'))
Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT
	from (irb):31:in `include?'
	from (irb):31
	from /Users/roblacey/.rvm/gems/ruby-2.2.2@upgrade-ruby2/gems/railties-3.2.22/lib/rails/commands/console.rb:47:in `start'
	from /Users/roblacey/.rvm/gems/ruby-2.2.2@upgrade-ruby2/gems/railties-3.2.22/lib/rails/commands/console.rb:8:in `start'
	from /Users/roblacey/.rvm/gems/ruby-2.2.2@upgrade-ruby2/gems/railties-3.2.22/lib/rails/commands.rb:41:in `<top (required)>'
	from script/rails:6:in `require'
	from script/rails:6:in `<main>'