[Feature][ActiveRecord] Belongs_to required option should a accept a Proc

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[Feature][ActiveRecord] Belongs_to required option should a accept a Proc

Nguyễn Đức Long
Whenever we write
belongs_to :association, required: true
or
belongs_to :association, optional: false
Rails will add a presence validator for that association.

My suggestion is that required or optional option should be able to take a Proc to be passed to the presence validator's if/unless option.
Example:
belongs_to :association, required: proc { condition }

# Equivalent to
belongs_to
:association
validates
:association, presence: true, if: proc { condition }

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Belongs_to required option should a accept a Proc

Andrew Kaspick
I'd say you want required: false with a condition on a validates_associated instead.

On Mon, Jun 11, 2018, 11:48 PM Nguyễn Đức Long, <[hidden email]> wrote:
Whenever we write
belongs_to :association, required: true
or
belongs_to :association, optional: false
Rails will add a presence validator for that association.

My suggestion is that required or optional option should be able to take a Proc to be passed to the presence validator's if/unless option.
Example:
belongs_to :association, required: proc { condition }

# Equivalent to
belongs_to
:association
validates
:association, presence: true, if: proc { condition }

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.