Is there a better way to do this (SQL groups)?

I have a Task model and a Category model with belongs_to and has_many.
What I want is to get from de DB the tasks in groups of categories and
each one of this groups with a maximum of 5 tasks. Later, I want to show
this in tables; each table will be a group.