Rails ActiveRecord provides an optional
option for belongs_to
. Consider the use case of allowing null for the foreign key and allowing the association to be null during object creation but requiring its presence during subsequent saves. For example, a new Member may have no initial Group, but any further updates of Member require a Group association.
Can the optional
option value itself be conditional? For example,
class Member < ApplicationRecord
belongs_to :group, optional: -> { new_record? }
end
behaves the same as optional: true
, and we can infer that the optional
option parsing only checks for a truthy value.
Is a custom validator the pragmatic way to meet this use case?
belongs_to associations are now automatically required: true So if the association is optional, that must be declared, or validation will fail without the presence of the associated model record.
What is optional true in rails? If you set the :optional option to true, then the presence of the associated object won't be validated. By default, this option is set to false . otherwise it will be required associated object.29-Aug-2020.
They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.
belongs_to means that the foreign key is in the table for this class. So belongs_to can ONLY go in the class that holds the foreign key. has_one means that there is a foreign key in another table that references this class.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With