Three Things I Learned This Week - January 20 2017

  • The belongs_to association is required by default in Rails 5. If I have a User/Profile relationship, I cannot create a Profile that doesn’t belong to a User (by default). I can make the association not required by declaring belongs_to :user, required: false. I have mixed feelings about this. Having the parent record be required for a child makes logical sense, and is almost always the behavior I want, but it makes testing harder. For example, if I want to test some model methods on my Profile model, I have to create an associated User model too, which makes the test build-up harder to configure, and slows down my test suite. I’m going to dig more into this to find the right balance.
  • Models in Rails 5 now inherit from ApplicationRecord instead of ActiveRecord::Base. This seems to simply add a thin layer in which functionality can be mixed-in for only the parent application and kept out of related engines, etc.
  • I finally dug into a long standing issue that some people were experiencing with SimpleCov, where the coverage stats were obviously incorrect. I had never seen the issue myself, so I couldn’t debug very well. Alas, I finally hit the issue myself and found that SimpleCov needs to be required before everything. In my default setup, I had it in the spec_helper.rb file which is itself required by rails_helper.rb. I simply moved the SimpleCov statements to the top of rails_helper.rb and the stats appear much more accurately.