Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Including Associated Data In A Sinatra API Response
Nic Mortelliti
Nic Mortelliti

Posted on

     

Including Associated Data In A Sinatra API Response

I just wrapped up development on aScrum/Kanban-like application based on Ruby and React. In this post, I'll be discussing one of the roadblocks I was up against when creating the Ruby application controller.


A scrum/kanban (we'll call it scrumban from here on out) board is used by development teams to provide a way to break down a project into smaller chunks of work that are able to be implemented in 2-to-4-week sprints. The scrumban boardtypically contains columns for work that is in the backlog, in-progress, in-review and closed.

My scrumban application allows a team to add new tasks to a project, edit a tasks properties (e.g. description, due date, story points, etc.) and delete tasks. All of the data for this application including users, project names, and tasks reside in their own tables in an SQLite database.

The scrumban React front end makes HTTP requests to the server through Sinatras application controller. Upon initial page load, the front end makes a request to the server for JSON object containing all of the tasks and their associated project and assigned user.THIS is where I hit a wall during development.

How do I set up the application controller routing in a way that includes an entities associated data frommultiple tables?

Image description

Problem

My Ruby models are set up as shown in the Entity-Relationship Diagram (ERD) shown below. We have a Projects table, a Users table and a Tasks table (our "Join" table). Each task has a single associated project and a single associated user assigned to the task. A project and user can have many tasks.
Scrum Board ERD

When it came time to configure my application controller (the "C" inMVC), I wanted to return a JSON object of all of the tasks in the database. I also wanted to include project and user information for each of these tasks. I knew how to access associated data fromone table (shown below), but how do I include data frommultiple tables?

1get'/'do2task=Task.all3task.to_json(4include: :project5)6end
Enter fullscreen modeExit fullscreen mode

I searched forums and read the Sinatra documentation, but I just couldn't find the solution. I'm sure the answeris documented out there somewhere. But I was struggled to find it.

Solution

One day I decided I was going to solve the mystery once and for all. I refused to build work-arounds into the client-side React application (e.g. multiple fetch requests, filtering arrays to find matching foreign and local keys, etc).

I threw everything I had at the problem. I tried multipleinclude: statements. I tried putting putting the associated tables into an object like thisinclude: {:project, :user}.

Finally, I tried an array. It worked! Putting[:project, :user] after theinclude: statement is we include associated data from multiple tables!

1get'/'do2task=Task.all3task.to_json(4include:[:project,:user]5)6end
Enter fullscreen modeExit fullscreen mode

Image description

This will now give us the associated projectand the assigned user for each task! It seems so simple now, but I was truly at a loss.

I hope this post helps someone else. If it does, let me know in the comments!

Photo byJoshua Fuller onUnsplash

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
majaliju profile image
Maj Aliju
  • Joined

Nic thank you so much! This was incredibly helpful -- had the same experience but you're right, the solution is seemingly nowhere to be found. Apart from this post now!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

The Flying Full Stack Developer | Extensive Engineering Experience | Real-Life Pilot
  • Location
    Oregon
  • Education
    Embry-Riddle Aeronautical University, Flatiron School
  • Work
    Systems Engineer and currently a Full-Stack Software Engineering Student
  • Joined

More fromNic Mortelliti

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp