Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

validates_inclusion/exclusion_of should also accept a symbol or proc as the :in/:within option

#873

validates_inclusion_of and validates_exclusion_of should also be able to accept a symbol representing an instance method or a proc that returns an enumerable object as the :in or :within option.

Example


class Page < ActiveRecord::Base
  acts_as_tree
  validates_exclusion_of :parent, :in => :self_and_all_children
  # or
  validates_exclusion_of :parent_id, :in => Proc.new { |page| page.self_and_all_children.collect(&:id) }

  def self_and_all_children
    self.children.inject([self]) { |array, child| array += child.self_and_all_children }
  end
end

Attached is a patch that adds this behavior to the validation methods in ActiveRecord and ActiveModel. I didn't see any tests for validations in ActiveModel, so I just added the tests for ActiveRecord.

Reported by shuber · August 20th, 2008 @ 08:26 PM

State: open
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Chris Cherry
    Chris Cherry

    +1

    This seems like a very useful addition, in fact I'm kinda surprised its not there already. The example he gives above is a pretty good demonstration of where it could be applied, checking for the existence of the value of the parent_id attribute in a dynamic set.

    Tests relating to this change pass.

    August 20th, 2008 @ 10:32 PM

  2. Pratik
    Pratik
    • State changed from new to incomplete

    I like this as well. A few changes before it can be pushed :

    • Missing ActiveModel tests
    • Missing tests for validates_exclusion_of
    • I don't like 'case enum.class.to_s'. It should just use 'case enum'

    August 21st, 2008 @ 11:25 AM

  3. shuber
    shuber

    Updated patch - uses 'case enum' instead of 'case enum.class.to_s'

    August 21st, 2008 @ 06:13 PM

  4. Stephan Kaag
  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Dan Pickett
    Dan Pickett
    • Tag changed from activemodel, activerecord, edge, patch, tested, validations to activemodel, activerecord, bugmash, edge, patch, tested, validations

    May 9th, 2010 @ 05:44 PM

  7. Dan Pickett
    Dan Pickett
    • No changes were found…

    May 9th, 2010 @ 05:44 PM

  8. Rohit Arondekar
    Rohit Arondekar
    • Importance changed from to

    Any updates here?

    July 4th, 2010 @ 03:09 AM

  9. Santiago Pastorino
    Santiago Pastorino
    • State changed from incomplete to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 05:01 PM

  10. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:01 PM

  11. pmontrasio
    pmontrasio
    • Tag cleared.

    It seems that shuber's patch didn't make it into Rails 3. Too bad because I was looking for a way to validate against some dynamic value. I'll fall back to writing a custom validator but it takes more code to do that.

    April 25th, 2011 @ 05:24 PM

  12. pmontrasio
    pmontrasio
    • State changed from stale to open

    [state:open]

    April 26th, 2011 @ 07:48 AM