4

I'm trying to validate that a has_many-through relationship has at least one value selected upon form submission. For simplicity, let's just call the relationship "relationship", and thus the ids "relationship_ids".

On my model, I included the following:

attr_accessible :relationship_ids
validates :relationship_ids, :length => {:minimum => 1}

Unfortunately, this does not work, as Rails forms includes an empty string in the array (i.e.: [""]) in case the user selects nothing, such that Rails knows to remove all associations that were set previously. There is no error, it's just the the length of relationship_ids is 1, and so the validation succeeds.

My next thought was that I could override the implementation of the relationship_ids= method, so I tried this:

def relationship_ids=(ids)
  super ids.reject(&:blank?)
end

Unfortunately, this results in a NoMethodError, specifically:

super: no superclass method `relationship_ids='

I'm thinking there's got to be a better/more correct way of doing this, and am looking for some input here. Thanks!

Edit: I already had a custom validator I was using previously. I've updated it to account for empty strings in the ids array. Here it is, in case this helps anyone else out.

class RelationshipValidator < ActiveModel::EachValidator
  CHECKS = { :is => :==, :minimum => :>=, :maximum => :<= }.freeze
  MESSAGES = { :is => :equal_to, :minimum => :greater_than_or_equal_to, :maximum => :less_than_or_equal_to }.freeze
  RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :greater_than_or_equal_to, :less_than_or_equal_to]

  def initialize(options)
    if range = (options.delete(:in) || options.delete(:within))
      raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range)
      options[:minimum], options[:maximum] = range.begin, range.end
      options[:maximum] -= 1 if range.exclude_end?
    end

    super(options)
  end

  def check_validity!
    keys = CHECKS.keys & options.keys

    if keys.empty?
      raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
    end

    keys.each do |key|
      value = options[key]

      unless value.is_a?(Integer) && value >= 0
        raise ArgumentError, ":#{key} must be a nonnegative Integer"
      end
    end
  end

  def validate_each(record, attribute, value)
    value = record.send(attribute.to_sym).reject(&:blank?).size

    CHECKS.each do |key, validity_check|
      next unless check_value = options[key]
      next if value && value.send(validity_check, check_value)

      errors_options = options.except(*RESERVED_OPTIONS)
      errors_options[:count] = check_value

      default_message = options[MESSAGES[key]]
      errors_options[:message] ||= default_message if default_message

      record.errors.add(attribute, MESSAGES[key], errors_options)
    end
  end
end

And to use it, here are a few examples:

validate :relationship_ids, :relationship => {:minimum => 1}
validate :relationship_ids, :relationship => {:maximum => 5}
validate :relationship_ids, :relationship => {:is => 2}
validate :relationship_ids, :relationship => {:within => 1..3}
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
  • If you just want to make sure at least one association exists, as you state in your first sentence, did you try validates_presence_of? See http://stackoverflow.com/questions/5689888/rails-validate-presence-of-association – ybakos May 09 '11 at 06:35
  • I didn't realize that was an option for has_many associations, I'll try that tonight. Thanks for the heads up1 – Matt Huggins May 09 '11 at 14:59

1 Answers1

-1

As noted (here), this Custom Rails Validations guide may help.

Community
  • 1
  • 1
Bryan Drewery
  • 2,569
  • 19
  • 13
  • Thanks. I already had a validator, but I wasn't sure if I should modify that, or if there was something else I should be doing. I updated my validator, and I included it (and its usage) in my question in case it helps anyone else out. – Matt Huggins May 09 '11 at 04:49