Validation of model object through a relationship

28 views
Skip to first unread message

john...@gmail.com

unread,
Apr 24, 2014, 3:50:06 PM4/24/14
to rubyonra...@googlegroups.com
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?

Colin Law

unread,
Apr 25, 2014, 3:27:41 AM4/25/14
to rubyonra...@googlegroups.com
On 24 April 2014 20:50, <john...@gmail.com> wrote:
> 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.

What is the data type of quantity in the database?

Colin

>
> 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?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/f16095c4-da9e-44bd-a094-f9539a99885c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

john...@gmail.com

unread,
Apr 25, 2014, 2:29:04 PM4/25/14
to rubyonra...@googlegroups.com
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)

Colin Law

unread,
Apr 25, 2014, 5:09:17 PM4/25/14
to rubyonra...@googlegroups.com
On 25 April 2014 19:29, <john...@gmail.com> 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
Reply all
Reply to author
Forward
0 new messages