Rails 4, phantom ArgumentError: wrong number of arguments (0 for 1) on UserSession.find for AuthLogic

This was a total pain to locate, as the exceptions being raised were pointed to UserSession.find in Authlogic::Session::Persistence. gems in question: activerecord-session_store 0.1.1 authlogic 3.4.6 activerecord 4.2.3 rails 4.2.3 I noticed that the only meaningful difference between the two environments was the following instance variable in Rails.application.config: irb> pp Rails.application.config . . . @logger= # […]

has_many :through, scoped to a value on the other side of the :through relationship

Here goes another round of wrapping my head around what rails associations are looking for. This time, I’m trying to scope a collection on the other side of a has_many :through to an attribute on that model. Contrived exhibit A: class Gym < ActiveRecord::Base # gym has a boolean "open" end class TrainerGym < ActiveRecord::Base […]

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 […]

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 […]