Looking for some suggestions on how to store my RoR data. Essentially, I have 20 or so items (will never go above 50) that I need to track. Each item has a different X,Y coordinate at each time of day (one of either Morning, Afternoon, Evening), and each of the seven weekdays it has a different schedule (essentially, 21 different x,y points for each time of day/day of week combination). It also has some static values, such as the name, category, and whatnot.
Right now I’m trying to decide how I want to store it, those 21 different x,y points. I suppose there are two opposite extremes on the spectrum – either (a) store each field like wednesday_afternoon_x, wednesday_afternoon_y, etc, or (b) serialize and store as a hash/struct, like x.wednesday.afternoon or something. A third option might even be to break them up into separate tables (ie have a :tuesday_item, :wednesday_item models, etc)
One thing that I do need to be able to do is search/filter by those fields – for example, if I want to get the items that are within a specific Category for a given TimeOfDay and DayOfWeek.
Are there any recommendations as to how I should proceed? Thanks!
I’d use two database tables, one for items, and one for coordinates. The schema then would be simple:
[UPDATE: When you actually create these migrations, don’t forgot to add indexes! For
item_coordinates, you probably want a joint index on[:item_id, :weekday, :time_of_day], but don’t take my word for that — figure out which fields you actually end up using as keys and index those.]Your
Itemmodel wouldhas_many :item_coordinatesand yourItemCoordinatemodel wouldbelong_to :item.Then you can use all the regular ActiveRecord methods. For example, to get a specific coordinate easily under this system: