How do you declare an optional condition for an ActiveRecord belongs_to association?
It looks like providing a lambda to the optional
option won't work (although I haven't tried it). I looked at the source code and this is how optional
is used.
required = !reflection.options[:optional]
If required, Rails just adds a presence validation like this:
model.validates_presence_of reflection.name, message: :required
I believe you could go the custom route with something like this:
class Member < ApplicationRecord
belongs_to :group, optional: true
validates :group, presence: true, on: :update
end