0

I'm new to Rails and coding in general, and I'm trying to teach myself by working on an app for a delivery driver. I have a table called loads, and included are the columns for Gross Weight, tare weight and net weight. The way it is currently, the driver would have to type in all 3 weights manually. I'm trying to figure out the best way to have the app calculate the net weight (by subtracting the tare weight from the gross weight)and add it to the net column. This would cut down on the driver making a simple math mistake. I'm using MySQL, can someone point me in the right direction?

3 Answers 3

3

I would add a before_validation callback to the model and let is calculate the missing value. Something like this:

# in your model
before_validation :calculate_net_weight

private
def calculate_net_weight
  self.net_weight = gross_weight - tare_weight
end

Btw. I would argue that is makes sense to store all three values in the database even if you could calculate one from the other two. But having all three values in the databse might help to fix problems or might make calculation or statistics easier.

Sign up to request clarification or add additional context in comments.

Comments

2

why keep that logic in the database? You could simply have it in the model.

Let's say you have a Load model

simply add the following method

def net_weight
  gross_weight - tare_weight
end

4 Comments

Forgive me if I'm sounding like an idiot, but I think it would be useful to have that data saved for each load, that way if we want to run a report/invoice or something for the last week, or the last month or even year. Or would I just have that method calculate it every time?
And I'm not sure why I'm getting the negative marks for my question. I've been trying to research this for a while, and have not been able to find anything. Maybe I'm so new at this that I'm not sure how to search what I'm looking for or how to ask the right question, but a little direction would help, I thought this was a community that helps each other out... Seeing all the negative marks with no feedback is a little discouraging.
Let's say for some reason in the future your logic changes or you add a layer of complexity, this function allows you way more flexibility than just stored data :)
as for the downvotes, it's mostly because you didn't provide much code/database insight. next time copy your model and your db/schema.rb :)
1

I would also ask why you have to store it in the database. If you do want to store it none the less:

You can create the before_create lifecyle hook of a model to calculate and set values before saving it to the database.

3 Comments

Thanks for the reply Axel. Why I have to store it in the database? Lol, cause I probably don't know any better. I just thought if I need to access that data later on, it would be best to have it stored...
to the why. IMO you'd store it in the database because its one computation stored (in the database) when the record is updated vs having to compute it every time on a render for every record. ruby/rails is slow enough already without unneeded computations in the view.
Storing this because 'ruby/rails is slow enough already' is a classic case of premature optimization - storing this value in the db does indeed yield less computations, but you also have to 'remember' to update this if you update the other weights - of course you can do this with a before_save, but this is more code. And the best code is the one you don't have to write ;-). So unless you have actual performance issues I would stick with the transient solution proposed by Alexandre as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.