Topic: Find a model grouped by association even if the association is nil

I am new to rails and MVC in general and I have a general question about model relationships. I hope this is the right place to post this.

I have 2 models

class Project < ActiveRecord::Base
  has_many :todos
end

class Todo < ActiveRecord::Base
  belongs_to :project
end

So, a Project can have 0 or more Todos. Also a Todo may or may not be associated with a Project. This is what I want and it all is well.

But what do I do when I want to display all Todos by Project. I want to have the Todos included that don't belong to a Project. I could accomplish this by:

  • Getting all the projects in one variable and all the todos without projects in another

  • Looping over the projects and displaying the project and the todos within

  • Then looping over and displaying the todos that don't belong to a project

This seems like such a common thing that there might be a Rails way to get this all inside one data structure. Some way to tell rails

Re: Find a model grouped by association even if the association is nil

... my post got cut off before I finished. Maybe I am too long winded. Continuing:

This seems like such a common thing that there might be a Rails way to get this all inside one data structure. Some way to tell rails "I want all the Projects, but what I am really after is Todos, so give me those in an empty Project if they have no Project."

Or should I be setting up a default project maybe called "misc" that all Todos get assigned to if one isn't specified? If so, should I specify the creation of that Project object in the Project model (can I tell Rails "When this program is run (for the first time) create a Project object called 'misc' because we are going to need it later"?). Or maybe that should be part of the Todo model: "Rails, before you save a Todo, see if it has a Project. If it doesn't, then assign it the 'misc' Project. If that object does not exist, then create it."

Any guidance is appreciated.

Thanks

Last edited by banderson (2012-02-17 12:10:30)

Re: Find a model grouped by association even if the association is nil

Unless there is an even simpler method, how I would do it is with 2 separate queries.

Todo.rb

class Todo < ActiveRecord::Base
  belongs_to :project

  # Add a scope for finding all todo's that aren't assigned to a project
  scope :unassigned, where('project_id = ?', nil)
end

Then, in your project's view file:

<h1>Projects and their todos</h1>
<% @projects.each do |project| %>
  <%= project.name %>
  <% project.todos.each do |todo| %>
    - <%= todo.name %>
  <% end %>
<% end %>

<h1>Unassigned todos</h1>
<% Todo.unassigned.each do |todo| %>
  - <%= todo.name %>
<% end %>

Does that make sense? I've not tested the above code, but it should work. If not, let me know. Hopefully you get the basic idea. smile

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: Find a model grouped by association even if the association is nil

Read the last two posts in this thread:

http://railsforum.com/viewtopic.php?id=47942

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Find a model grouped by association even if the association is nil

Great, thanks for the help guys. Looks like I will need to load Projects and unsigned Todos into separate data structures, but scopes makes it easy and DRY.

Thanks again.