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.

On Fri, Dec 5, 2014 at 5:02 PM, Griffin Smith <wildgriffi...@gmail.com>
wrote:

> 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
>
>  --
> 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 rubyonrails-core+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-core@googlegroups.com.
> Visit this group at http://groups.google.com/group/rubyonrails-core.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
At.
Carlos Antonio

-- 
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 rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to