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