# Newbie question: Custom Validations

**URL:** <https://www.ruby-forum.com/t/newbie-question-custom-validations/168459>\
**Category:** Rails\
**Created:** [May 21, 2009, 12:29am UTC](https://www.ruby-forum.com/t/newbie-question-custom-validations/168459 "2009-05-21T00:29:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cardinallijr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@cardinallijr](https://www.ruby-forum.com/u/cardinallijr)\
**Post date:** [May 21, 2009, 12:29am UTC](https://www.ruby-forum.com/t/newbie-question-custom-validations/168459/1 "2009-05-21T00:29:49Z")

</div>

Hi all,

I’m trying to validate my model through equal values occuring a number  
of times on the same column. I think that I have to use a custom  
validation, right? I tried to use it but had no success.

For example, that’s the table:

class MyTest \< ActiveRecord::Migration  
def self.up  
create\_table :mytest do |t|  
t.column :name, :string  
t.column :column1, :boolean  
end  
def self.down  
drop\_table :products  
end  
end

What I need is that when 2 records on the table ‘mytest’ had column1 =  
TRUE (1), and we try to insert a new record with this condition  
(column1 = TRUE), an error to this field be added. Please, can you  
help me to find a solution for this? Any hint for the model file  
(mytest.rb)?

Thanks for the help.

–  
Alberto

---

<div class="post-metadata">

**Author:** ![cardinallijr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@cardinallijr](https://www.ruby-forum.com/u/cardinallijr)\
**Post date:** [May 21, 2009, 9:12am UTC](https://www.ruby-forum.com/t/newbie-question-custom-validations/168459/2 "2009-05-21T09:12:24Z")

</div>

No doubt there are neater ways but you could define a named scope that  
finds  
all records where column1 is true, then in your validate method if  
column1  
for the new record is true and if your named scope.count \> 1 then add an  
error for column1. The named scope is not compulsory, it is just for  
tidiness.

You may have to worry about what happens if two people try to update the  
db  
at the same time though. I am not sure of the best way to solve that  
one.

2009/5/20 cardinallijr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cardinallijr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@cardinallijr](https://www.ruby-forum.com/u/cardinallijr)\
**Post date:** [May 21, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/newbie-question-custom-validations/168459/3 "2009-05-21T17:58:26Z")

</div>

Colin,

I understood what you suggested about using named scope and I’ll try  
thislater. About the concurrency case, I don´t need to worry about it  
because it is a “admin” functionality, so only one user will use this.

Thanks for your help.

–  
Alberto

---

<div class="post-metadata">

**Author:** ![cardinallijr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@cardinallijr](https://www.ruby-forum.com/u/cardinallijr)\
**Post date:** [May 26, 2009, 5:48am UTC](https://www.ruby-forum.com/t/newbie-question-custom-validations/168459/4 "2009-05-26T05:48:23Z")

</div>

Colin,

Thanks man, It worked here. Only to register what I did:

1. Created a :named\_scope on my model ( for example:  
named\_scope :my\_test, :conditions { :column1 =\> true } )
2. On my validate method ( for example: errors.add(:column1, “should  
be \>= 2”) if column1 == true && MyTest.my\_test.count \> 1 )

Don’t know if this the most elegant way to do this but It really  
worked.

Thank you.

–  
Alberto
