As you may have read I like to use Postgres’ official image in my multi-container Docker environment. Out of the box it needs little configuration. I typically access it through my Rails app using rails db or rails console . The other day, however, I needed to craft some SQL and those tools weren’t cutting it. I wanted to use my SQL client so I could work directly with the schema on something better than the command line. That required some tweaking to my DB container. By default the Postgres image uses Docker’s EXPOSE instruction to open port 5432 for other Docker containers. EXPOSE does not make the port…
Leave a CommentCategory: dev
The purpose of NotImplementedError is defined in the Ruby docs: NotImplementedError is raised when a feature is not implemented on the current platform. For example, methods depending on the fsync or fork system calls may raise this exception if the underlying operating system or Ruby runtime does not support them. So whenever I see code like this:
1 2 3 4 5 6 7 8 9 |
class Vehicle def manufacture(n) n.times { create } end def create raise NotImplementedError, "subclass did not define #create" end end |
I cringe. Raising NotImplementedError simply to signal that a subclass didn’t abide by the contract of its superclass is an abuse of the error. The error is intended to be raised when a method cannot be implemented due to a limitation of the underlying…
3 CommentsI found a bug in my code today that stems from my not remembering that ActiveRecord::Base will try to coerce values set on its attributes into whatever type is defined in the DB. Specifically I got burned by #id= coercing a String into a Fixnum. Check this out:
1 2 3 4 5 6 |
[10] pry(main)> record = ActiveRecordSubclass.new => #<ActiveRecordSubclass:0x00555d2e588a28 ... > [11] pry(main)> record.id = "09378476376282" => "09378476376282" [12] pry(main)> record.id => 9378476376282 |
Not only is the type of object changed by setting the attribute but the leading zero is lost. Luckily my specs caught this. I know, I know it’s a bad idea to set #id yourself. You should let ActiveRecord do that for you. In my case I was trying to cleverly avoid…
Leave a CommentI got bit by the Rails production environment today. Here’s the scenario: I have a set of classes that all mixin the same module.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
module Syncable ... end class DownloadCars include Syncable ... end class DownloadTrucks include Syncable ... end |
Upon include the module “registers” the class with another class
1 2 3 4 5 6 7 8 |
module Syncable extend ActiveSupport::Concern included do |base| ApiCheck.registry[base.api_url] = base end ... end |
Once registered subclasses of ApiCheck can perform actions using the registry. Those actions are run via rake tasks. In development, when classes are autoloaded (i.e. only loaded when referenced), the rake tasks work fine. In production, however, the tasks fail because, in the context of a rake task, ApiCheck.registry is empty. None of my classes were registered. This struck me as odd because in production Rails eager loads…
2 Comments