# Stuck on validation

**URL:** <https://www.ruby-forum.com/t/stuck-on-validation/91935>\
**Category:** Rails\
**Created:** [April 13, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/stuck-on-validation/91935 "2007-04-13T16:22:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wintermute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@wintermute](https://www.ruby-forum.com/u/wintermute)\
**Post date:** [April 13, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/stuck-on-validation/91935/1 "2007-04-13T16:22:41Z")

</div>

Hello,

I am brand new to RoR trying to use the framework to build a simple  
web application (for an imaginary online shop) just to learn the  
ropes. Once every so often I come upon some problem that seems to have  
a really simple solution but I just cannot see it because of my  
inexperience. I was hoping you could help me out…

So here’s the issue. I have a simple mySQL database and a scaffold  
model for it. The database will contain products with a name and two  
different prices (eg. per unit and per kg). However the products  
themselves have 5 different price type combinations (each, unit, kg,  
100g, 100ml). Each product has to have at least 2 out of the 5 prices,  
but I will be importing them directly and I I have no control of which  
2.

I am trying to set up the validation for this, but I have no idea how.  
It needs to check that at least 2 of the 5 prices are present, but it  
does not matter which. The railes validates\_ methods seem a bit simple  
for this, but maybe I’m just not seeing it. When the controller  
invokes the create method is there any way to use the params passed in  
the validation? If not then any idea how I can solve this problem?

Thanks for any and all help,

W

---

<div class="post-metadata">

**Author:** ![wintermute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@wintermute](https://www.ruby-forum.com/u/wintermute)\
**Post date:** [April 13, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/stuck-on-validation/91935/2 "2007-04-13T16:33:42Z")

</div>

Hi Wintermute,

> does not matter which. The railes validates\_ methods seem a bit simple  
> for this, but maybe I’m just not seeing it. When the controller

You’re right: the ActiveRecord::Validations::ClassMethods are very  
specific. I think what you really want to do is override validate(),  
as described here:

[http://api.rubyonrails.com/classes/ActiveRecord/Validations.html#M000930](http://api.rubyonrails.com/classes/ActiveRecord/Validations.html#M000930)

That gives you the flexibility to examine any combination of fields of  
your model object to decide whether it’s valid or not.

Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![wintermute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@wintermute](https://www.ruby-forum.com/u/wintermute)\
**Post date:** [April 13, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/stuck-on-validation/91935/3 "2007-04-13T17:13:44Z")

</div>

Thanks Dave,

Just wondering - the mySQL price data is stored as a decimal(6, 2).  
Running .class on it in Ruby gives “Bigdecimal”. However I could not  
find this class anywhere in the Ruby or Rails APIs… how come?

---

<div class="post-metadata">

**Author:** ![wintermute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@wintermute](https://www.ruby-forum.com/u/wintermute)\
**Post date:** [April 14, 2007, 3:40am UTC](https://www.ruby-forum.com/t/stuck-on-validation/91935/4 "2007-04-14T03:40:43Z")

</div>

> Just wondering - the mySQL price data is stored as a decimal(6, 2).  
> Running .class on it in Ruby gives “Bigdecimal”. However I could not  
> find this class anywhere in the Ruby or Rails APIs… how come?

It’s not in Ruby core – it’s in Ruby standard libs:

[http://www.ruby-doc.org/stdlib/](http://www.ruby-doc.org/stdlib/)

Regards,  
Dave
