Conditions on a has_many :through and ActiveRecord 4.1 enum

Say you have a set of models (model names sanitized for generic posting) where the join table in the middle of a has_many :through has an enum that you want to add a filtered association for. class Student < ActiveRecord::Base has_many :student_grades has_many :grades, through: :student_grades end class StudentGrade < ActiveRecord::Base enum approval_status: [:not_reviewed, :rejected, […]

has_many :through, self referential models

Hat tip to [has_many :through self-referential example] that sent me down the correct path on this. The challenge I had went a little deeper than the aforementioned post, however. I’m going to reuse the friend example, but with a twist. class Person < ActiveRecord::Base # id :integer end class Friendship < ActiveRecord::Base # source_friend_id :integer […]

Principle of Least Surprise (Astonishment), foreign keys, and Rails

In yesterday’s post, I sorted through the foreigner gem to figure out how to change the reference column (primary key) that a foreign_key maps to. The problem here is that, unless your Rails project(s) has grown up referencing “natural” primary keys instead of the autoincremented id implicit in an ActiveRecord::Migration, avoiding creating surprise by not […]

foreigner foreign_key on a different column in source and target table.

In the foreigner gem, you can specify the foreign_key column on the source table by using the column option. class Cats < ActiveRecord::Migration def change create_table :cats do |t| t.integer :color_id t.integer :second_color_id t.timestamps end add_foreign_key :cats, :colors, column: :second_color_id end end But what about the target table? This can be accomplished using the primary_key […]

includes and has_many :through associations

I’ve been trying to mentally absorb how includes and associations work when trying to query a has_many :through association. My understanding at this point: The symbol in the includes() is the same as the association name on whatever model you have. For example: class Account < ActiveRecord::Base has_many :account_destinations has_many :destinations, through: :account_destinations end class […]

Rails 4, Empty Hashes, Strong Parameters, exception_notification and filtering out my new ActionController::ParameterMissing errors

I’m finally coming around to the “strong parameters” pattern encouraged (required?) in Rails 4. One thing that I did notice was that missing the base parameter raised an ActionController::ParameterMissing error. I discovered this via controllers tests generated by rspec-rails when I created a controller scaffold, which generates tests for “Invalid Params”: In the controller spec: […]

Bundler reports errors across several different projects when running `bundle install`

When running bundle install, I was getting the following for several projects: Bundler gave the error “Could not find {gem_name_version} in any of the sources” while processing “{project_path}/Gemfile”. Perhaps you forgot to run “bundle install”? . . . I found someone else was having the same problem too. In my case, I had uninstalled and […]

method_missing: undefined method `increment_open_transactions` database_cleaner

When using database_cleaner (0.9.1) with rails (4.1.0) {my home path}/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activerecord-4.1.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing’: undefined method `increment_open_transactions’ for ActiveRecord::Base:Class (NoMethodError) from {my home path}/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/database_cleaner-0.9.1/lib/database_cleaner/active_record/transaction.rb:13:in `start’ from {my home path}/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/database_cleaner-0.9.1/lib/database_cleaner/base.rb:73:in `start’ from {my home path}/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/database_cleaner-0.9.1/lib/database_cleaner/configuration.rb:75:in `block in start’ from {my home path}/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/database_cleaner-0.9.1/lib/database_cleaner/configuration.rb:75:in `each’ from {my home path}/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/database_cleaner-0.9.1/lib/database_cleaner/configuration.rb:75:in `start’ Somehow we were using an old version of database_cleaner, gem “database_cleaner”, […]