Neutral ground

In war; soldiers go prepared with suitable equipment, knowledge, and strategy to tackle the enemy. This is to ensure the best possible outcome rather than to be surprised. As Sun Tzu said in Art of…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to access associated models in Sinatra with Active Record controller

If you are new to Sinatra and Active Record and getting confused about accessing associated models, you are not the only one. I recently finished my very first Sinatra project at Flatiron School and took some time searching how I can specify multiple associations that are on the same level.

In this blog, I will cover the followings:

I will be using my Sinatra project as an example and I hope this will help someone who run in to the same problem as me!

I built an App that a user can keep track of baby’s daily records such as milestones, doctor appointments, and immunizations using Ruby/ Sinatra/ Active Record back end and React front end. Here is what ERD looks like:

We have four models: Baby, Milestone, Immunization, DoctorAppointment.

Baby has many Milestones,Immunizations, and DoctorAppointments, and they belongs to a Baby. Therefore this is a one to many relationship.

“:include” will allow fetching not only Baby table but also associated table as well which is Milestone in this example. The result of this call will display this:

Think about how you want to display your data in JSON response. Since this is one to many relationship, Milestone, Immunization, and DoctorAppointment belongs to Baby. Each of associated models will display its own nested data in JSON response. Since all the associated models are on the same level, you can include them in array which is pretty simple!

This will produce the following response:

Perfect!

Let’s suppose we want to add another model User. We would have many to many relationship and completed ERD would look like this:

So how can we include theUser associated with Milestones in JSON response? Again, think about how you want to display data in JSON response. You want to access all the babies along with its associated baby records and also be able to see the user tied to each record. In other words, you want to see Baby including Milestones including User . Saw the hint? Yes, you can use pass another “include:” inside hash!

It’s so much easier to manipulate JSON response structure if you think about where and how you want to display data based on what’s needed for the front end. In this case, we wanted to include two nested data which is Milestone and its associated User inside Baby data in JSON response. Therefore, first “include:” points to a hash. “milestone: “ inside this hash is acting as a key and points to another hash that contains another “include:” with its associated table which in this case is a User . The result of this call will produce the following:

This time, JSON response includes associated user data. Phew!

One thing you need to remember is to set up Active Record model correctly. Without correct set up, it will not work. For example, Baby model should look something like this:

Happy coding!

Add a comment

Related posts:

A Dream of Atlantis

Standing at the hill of Palonkon, looking west towards the ocean, I saw the entire western sky moving, clouds swirling into whirlpools; Space Jets taking off from the city of Saint Philip, due north…

Of Mountains

In response to Haiku This! Mountain by S Lynn Knight. “Of Mountains” is published by Kittie Phoenix in Haiku Hub.

Google dominates the Data Science Market

During the last few years, a lot has changed in the daily work of employees. Instead of working on desktop PCs with programs such as MS Word or Excel installed, they now work on Smartbooks from home…