Validation of model object through a relationship

I’m a beginner in Rails working through the Hartl tutorial while trying
to
work on my own project(s) at the same time.

One Project I’m trying to work on is a basic ‘assembly’ application
because
it’s what I do now full time.

Right now it’s a pretty basic app with 3 Models

Ingredient
Bulk
RecipeItem

There is a has_many through relationship between Ingredient and Bulk
through RecipeItem

In the recipe_items table there is an additional attribute i’m calling
quantity - it must be greater than 0 and less than 1.

The validation question I have is this.

Is there an easy way to set up a validation on a Bulk object that
requires
its related RecipeItem objects to add up to one…after all the
recipeitem
objects are added?

On 24 April 2014 20:50, [email protected] wrote:

RecipeItem

There is a has_many through relationship between Ingredient and Bulk through
RecipeItem

In the recipe_items table there is an additional attribute i’m calling
quantity - it must be greater than 0 and less than 1.

What is the data type of quantity in the database?

Colin

I don’t know the exact set up but it’s a float - the sum either needs to
add up to 1 or 100 depending on how i set up the recipes. The only
reason
I’m doing this is I need a beginner project to work on and this is
something i know - and the blog idea keeps bogging down.

After more thought - here’s what ideally i think i would like to do.

Create a New Bulk - when ready click a button or link that says ‘enter
recipe’.

The new recipe page has an add ingredient button that by ajax adds
another
line to the ‘recipe’ div on the end - it draws ingredients (via ajax)
from
the ingredient database, there’s an outer div that would sum all the
ingredient quantities and not allow the submit button to work until it
adds
up to the right number.

Does that seem like the way to go about it?

(at this point - all ingredients would be in lbs)

On 25 April 2014 19:29, [email protected] wrote:

I don’t know the exact set up but it’s a float - the sum either needs to add
up to 1 or 100 depending on how i set up the recipes. The only reason I’m
doing this is I need a beginner project to work on and this is something i
know - and the blog idea keeps bogging down.

Please remember to quote the previous message. No-one reading this
will know what you are talking about.

If it is a float you cannot test for the total exactly equal to one.
You can never rely on floats being exactly equal to anything, due to
rounding errors.

My advice is to finish working right through the tutorial before
working on your own project.

Colin