It seems like adding an invalid
belongs_to association to a model silently returns nil when that association is invoked.
In the example below, there is no
bar_id column in the
foos table. There’s not even a
class Foo < ApplicationRecord belongs_to :bar end Foo.last.bar # => nil
This behavior persists even when explicitly specifying an invalid
foreign_key to belongs_to.
I’m sure there’s a reason for this but it can be very confusing. It mislead me to believe I had missing database record when in fact I had just made a typo.
This is also not consistent with the behavior of
has_one which will error if the association can’t be mapped back to an existing model.