Stuck on validation

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

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

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

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?

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/

Regards, Dave