Validate using boolean values

The Problem: how to do the validation. I have not written it
correctly!!!

Given two tables: sizes and prices;
with a relation: has_many ,belongs_to

a Price.unit_price for a given Size.meassure must be
marked ‘true’ as the standard unit_price for that meassure.

There will be many other unit_prices for a given meassure, those must be
marked ‘false’

table sizes
meassure, :string (ie.small, medium, big)

Model Size
has_many :prices

table prices
price_name :string
unit_price :integer
standard :boolean
size_id :integer

Model Price
belongs_to :size

I have tried both of this validation options in the Price model,
first one, then the other and none works for my stated needs.

validates_uniqueness_of :standard, :scope => :size_id,
:message => " : There is a standard price
for this meassure"

(this one fails because it just allows ONE ‘true’ in a :standard
field in the entire
database for ANY value of size_id; so once there is a ‘true’ in the
:standard field of the whole database, it will not allow any more;
so tha does not fit me…)

protected
def validate
errors.add(:standard, " : There is a standard price for this
meassure") if
size_id and standard == ‘true’
end

(this one fails because it allows to have many ‘true’ values in
the :standard
field of the database)

This is what i need to get:

I am using MySQL, notice that MySQL writes ‘true’ = 1 and ‘false’ = 0;
also see that for a given unit_price for a meassure
there can only be one standard = ‘true’=1 value.


                   sizes  (in the Size model

validates_uniqueness_of :meassure)


id meassure

1 Small
2 Medium
3 Big


                   prices

id price_name unit_price standard size_id

1 normal 1250 1 1
2 sale 500 0 1
3 summer 900 0 1
4 wholesale 1000 0 2
5 normal 1900 1 2
6 winter 1500 0 2
7 normal 3000 1 3
8 thanksgiving 2500 0 3
9 sale 1000 0 3

Please help me write up the correct way to validate this problem

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs