Method for combining :if and :unless on validates when nesting with_options

I feel like one of the more common use cases for with_options is an :if or an :unless argument to validates, like so:

with_options if: :something? do validates :thing, presence: true validates :other_thing, numericality: { greater_than: 7 } end

``

This breaks down when trying to nest multiple with_options blocks, because the second :if overrides the first when calling Hash#merge. It would be nice if I could do:

with_options if: :something? do validates :thing, presence: true validates :other_thing, presence: true

with_options if: :something_else? do validates :third_thing, presence: true # only runs if something? && something_else? end end

``

Currently to get this to behave as expected I have to do:

with_options if: :something? do validates :thing, presence: true validates :other_thing, presence: true end

with_options if: → { something? && something_else? } do validates :third_thing, presence: true # only runs if something? && something_else? end

``

I think you can also combine multiple conditions with an array of methods to be called:

with_options if: [:something?, :something_else?] do

end

It’s a bit simpler/cleaner than the block form, but I don’t think that works in a nested way as you are imagining (and I think it might be better to just keep it as is).

Hope that helps.