Geo
Geo

Reputation: 96767

How could this be modeled from a database perspective?

I have a model Project, which has a start date and an end date. A project can have multiple objectives, and each objective should have a status ( succesful or not ) for every day.

So, for example, let's say I have a project named "Get in better shape", which starts today, and ends in 1 month. I add the following objective "Run 1 km". For every day of the project I want to mark the objective as succesful or failed. I was thinking that the result should be a different model, belonging to objective. Roughly, I have this:

Project   has_many   :objectives
Objective belongs_to :project
          has_one    :result
Result    belongs_to :objective

The thing is, I was also thinking of maybe adding the Day as a model, because right now it feels very complicated to show a certain day ( and it's results ) ... but somehow it wouldn't feel right.

Is there a simpler way of representing the models? How are you handling apps where date ranges are relevant/important ?

Upvotes: 1

Views: 55

Answers (1)

indiche
indiche

Reputation: 221

I was working on something similar recently. You can set it up like this:

Project     has_many     :objectives
Objective   belongs_to   :project
            has_many     :result
Result      belongs_to   :objective

Then use cron to create entry in Result database each day. Rails sets up timestamps in database by default, so filter results for a certain day by using created_at field.

You can use whenever gem to set up cron jobs.

Upvotes: 1

Related Questions