I want to create a rails app to track vehicle mileage. I figure this
would be a good first project to learn how to use rails. But I'm
hitting a snag.
I want to have a model for refueling events. They'd have a date,
odometer reading, gallons, cost, etc. But the problem is that for each
event there is a number of miles elapsed since the previous event.
(This is the miles travelled) I don't want to force the user to do
that math, I just want to enter the odometer reading at each event.
I can think of two ways to handle this, and I don't like either. I'm
hoping to get some feedback on the "correct" way to do this.
Possibility one: Have an elapsed miles property on the event, but not
stored in the data base. Then whenever a collection of events is
loaded from the db, a helper method iterates over the events, and
calculates the correct elapsed miles for each one.
- Seems like I'd have to do this iteration a lot. And I want to be DRY
Possibility two: Store the elapsed miles in the database. When a new
event is created, attempt to find the previous event (it'll be the one
with the highest odometer reading) and set the elapsed miles from the
current - previous odometer settings.
- Assumes I'd have to enter the events in order, and what happens when
a user edits an event somewhere in the middle, and changes the
Admittedly, I'm a noob and I come from a java background, so my brain
hurts as I try to transition into the ruby/rails way of thinking. Any
suggestions will be helpful and educational.