Question: I'm a Rails beginner and to learn it I'm building a simple time tracking app. I want to populate an administrator's dashboard with a ton of information from many tables with nested information.

What would be the best practice for querying the database to request all of the data for one company to view a dashboard of all clients, projects, tasks, adjustments and minutes?

Here's how the data is structured:

Company
has\_many clients

Client
belongs\_to company
has\_many projects

Project
belongs\_to client
has\_many tasks

Task
belongs\_to project
has\_many minutes

Minute
belongs\_to task

---

This data structure might be really bad. I don't know.

An example view of the data:

Activision  

 -- Website Redesign  

 --- Development  

 ---- 100 Minutes 

I'm starting with this but I'm pretty but it could be totally backwards (Users belong to Companies):

```
    @clients = Client.find_all_by_company_id(current_user.company_id)
    @clients.each do |client| 

        project = Project.find_all_by_client_id(client.id)
        puts project.name

        project.each do |project|

            task = Task.find_all_by_project_id(project.id)
            puts task.name

        end
    end

```

I guess the question can also be asked: Is there a good book or resource that fully describes Rails ActiveRecord best practices?

Answer: 
This produces the same you have

```
@clients = current_user.company.clients
@clients.each do |client| 
    projects = client.projects

    # puts project.name # makes no sense here

    projects.each do |project|

        project.tasks.each do |task|
            puts task.name
        end
    end
end

```
try something like

```
 Client.includes(
                      :company =>{:projects=>:tasks})

```

all of the above should be connected (via has\_one, has\_many, belongs\_to)
Hope this helps!