0

Developing a web application that will act similar to a daily journal. The user can maintain several different lists of data for a given date. For the sake of discussion say we have two models, Notes and Tasks.

I would like to present a view to the user of dates they can navigate through showing the data for each date. Maybe in a strictly list view such as:

Jan 1
======
Tasks: 1 - First task is here
       2 - Second task is here

Notes: 1 - First note is here


Jan 4
=====
Tasks:  No tasks

Notes:  1 - A note is here.
        2 - Another note...

<< Older Entries        Newer Entries >>

Or maybe presented as a calendar having dates which have corresponding data marked or highlighted somehow.

I started down the path building out my models as just Notes and Tasks, each having a journal_date on them. This works fine when navigating each model index independently. Such as myapp.com/notes/ or myapp.com/tasks/ But I'm not sure how difficult it will be to consolidate each list into one view grouped by date.

I also considered having a model called JournalEntry, which has a journal_date, and has_many notes and has_many tasks. Then the notes and tasks wouldn't have a date, but they would each belong to a journal_entry which has a date. I think that would simplify things a bit. But I've read a few blog posts which say stay away from nested resources if possible. So something like: myapp.com/nick/journal_entries/2012-dec-23/tasks/ they say to stay away from because of the code becoming complex.

So I'm wondering how some more experienced rails developers would approach this problem? Which approach would you use, or would you handle it completely differently?

Nick

1 Answer 1

1

One way to do it without changing your model setup would be to group each model scope on the journal_date column and then loop through a unique list of the keys from the group hash, like this:

# Controller
since_date = DateTime.now - 30.days
@tasks = Task.where("journal_date > ?", since_date).group_by(:journal_date)
@notes = Note.where("journal_date > ?", since_date).group_by(:journal_date)
@date_entries = (@tasks.keys + @notes.keys).uniq

# View
<% @date_entries.each do |date_entry| %>
  <h1><%= date_entry.to_s %></h1>
  <h2>Tasks</h2>
  <% if @tasks[date_entry].any? %>
    <% @tasks[date_entry].each do |task| %>
      <p><%= task.title %></p>
    <% end %>
  <% else %>
    <p>No Tasks</p>
  <% end %>

  <h2>Notes</h2>
  <% if @notes[date_entry].any? %>
    <% @notes[date_entry].each do |note| %>
      <p><%= note.title %></p>
    <% end %>
  <% else %>
    <p>No Notes</p>
  <% end %>
<% end %>

The since_date part is of course just an example of putting some conditions for the retrieved records.

And the group_by(:journal_date) will only work if the column is of the type Date and not a DateTime. If it is a DateTime then you can solve it like this instead:

group_by { |journal_date| journal_date.to_date }
2
  • Thanks I'll read up more on group_by. I'm not opposed to changing my model up, so if there are some advantages to having a parent model entity to tie everything together I would consider that as well, any thoughts on that or is it best to keep the models independent? Jan 4, 2013 at 18:24
  • Just by the information from your question, I can't really see any advantage of having a separate parent model. But that is from a classic notes and task perspective. If you feel that you might benefit from it, then try it out.
    – DanneManne
    Jan 5, 2013 at 2:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.