# Validates\_exclusion\_of

**URL:** <https://www.ruby-forum.com/t/validates-exclusion-of/48257>\
**Category:** Rails\
**Created:** [November 18, 2005, 8:56am UTC](https://www.ruby-forum.com/t/validates-exclusion-of/48257 "2005-11-18T08:56:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@deepak](https://www.ruby-forum.com/u/deepak)\
**Post date:** [November 18, 2005, 8:56am UTC](https://www.ruby-forum.com/t/validates-exclusion-of/48257/1 "2005-11-18T08:56:23Z")

</div>

I am writing a very simple personal expenses calculator. each expense  
aka dealing can be of the type:

dealing name: albertsons  
payer : deepak  
recievers : akash,sid  
amt : 15 USD

so akash and sid each owe me 5 USD each. many such dealings and at the  
end of the mth or so you can find out how much you are in credit/debit.

now…my defintions are:  
– every dealing can have one and only one payer  
– every dealing can have many receivers

Models consist of: Dealing,Party(represents both payer and receiver) and  
Money(reps 15,USD)

and my dealing class looks like this:

class Dealing \< ActiveRecord::Base  
belongs\_to :payer, :class\_name =\> “Party”, :foreign\_key =\> “payer\_id”  
has\_and\_belongs\_to\_many :receivers,:class\_name =\> “Party”, :join\_table  
=\> “receivers”, :association\_foreign\_key =\> “party\_id”  
composed\_of :money, :mapping =\> [[:amount, :amount],[:currency,  
:currency]]

validates\_presence\_of :name,:occured\_on,:payer,:receivers,:amount

validates\_numericality\_of :amount

validates\_exclusion\_of :payer,:in =\> :receivers, :message =\> “Payer is  
also same as receiver”

validates\_each :receivers do |record,attr,value|  
record.errors.add attr,‘are repeated’ if value.uniq.size != value.size  
end

def amt\_per\_head  
if payer\_inclusive == ‘Y’  
Money.new(money.amount / (receivers.size + 1))  
else  
Money.new(money.amount / receivers.size)  
end  
end

def validate  
errors.add “Payer is same as receiver! Either” if receivers.include?  
payer  
end

protected :validate

This validation:

validates\_exclusion\_of :payer,:in =\> :receivers, :message =\> “Payer is  
also same as receiver”

fails and this is what irb tells me::

D:\rubywork\projects\rails\ezexpense\>ruby script/console  
Loading development environment.

> > deal = Dealing.new  
> > ArgumentError: An object with the method include? is required must be  
> > supplied a  
> > s the :in option of the configuration hash  
> > from  
> > d:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active\_recor  
> > d/validations.rb:551:in `validates_exclusion_of' from ./script/../config/../app/models/dealing.rb:10 from d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.1.1/lib/active_suppo rt/dependencies.rb:193:in`load’  
> > from  
> > d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.1.1/lib/active\_suppo  
> > rt/dependencies.rb:193:in `load' from d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.1.1/lib/active_suppo rt/dependencies.rb:38:in`require\_or\_load’  
> > from  
> > d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.1.1/lib/active\_suppo  
> > rt/dependencies.rb:21:in `depend_on' from d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.1.1/lib/active_suppo rt/dependencies.rb:171:in`require\_dependency’  
> > from  
> > d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.1.1/lib/active\_suppo  
> > rt/dependencies.rb:171:in `require_dependency' from d:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.1.1/lib/active_suppo rt/dependencies.rb:183:in`const\_missing’  
> > from (irb):1

However if i remove this validation and use validate things are fine.

I dont know why? Any pointers? Why does validates\_exclusion\_of not able  
to convert the symbol :receivers into deal.receivers :-(?

---

<div class="post-metadata">

**Author:** ![deepak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@deepak](https://www.ruby-forum.com/u/deepak)\
**Post date:** [November 19, 2005, 8:25am UTC](https://www.ruby-forum.com/t/validates-exclusion-of/48257/2 "2005-11-19T08:25:04Z")

</div>

Hi !

2005/11/18, Deepak S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I dont know why? Any pointers? Why does validates\_exclusion\_of not able  
> to convert the symbol :receivers into deal.receivers :-(?

validates\_inclusion\_of and validates\_exclusion\_of only work with  
static arrays (or enumerables). You’ll need to write the validation  
rule yourself:

validates\_each :payer do | record, attr, value|  
record.errors.add(attr, ‘is part of the receivers list’) if  
record.receivers.include?(value)  
end

Hope that helps !

---

<div class="post-metadata">

**Author:** ![deepak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@deepak](https://www.ruby-forum.com/u/deepak)\
**Post date:** [November 21, 2005, 8:13pm UTC](https://www.ruby-forum.com/t/validates-exclusion-of/48257/3 "2005-11-21T20:13:27Z")

</div>

francois.beausoleil wrote:  
Hi Franc,

A ticket has been opened…[http://dev.rubyonrails.org/ticket/2935](http://dev.rubyonrails.org/ticket/2935)

Also your solution works. Thanks.

I had tested with a fix on validations.rb. Just like validates\_each uses  
an attr,value combo validates\_exclusion\_of doesnt convert the enum into  
a record.send(attr). Hence we cant use symbols. Howver in 1.1 we can 🙂

Cheers  
Deepak S.
