Sinatra, Active Record and Associations. 
Published: 2020-06-26 19:13:42

<i>As a Software Engineering student, the idea of creating your first “real” web application is like a dream come true. I couldn’t be more excited about learning how to use Sinatra and I couldn’t wait for project week.<i> 

What is Sinatra? Sinatra is an open-source software web application library and DSL (<i>domain-specific language</i>) written in our old and good friend Ruby. Sinatra focuses on creating web-applications quickly and in some level “easy”. I said on some level because yes, technically building the app structure with Sinatra is easy, but if you don’t know how to work with your MCV (<i>models, controllers and views</i>) it’s pretty much impossible to build a functional app. 

Something that has been a challenge for me since we started learning about it is Associations. I understand the idea and make total sense, but for some reason was (<i>and sometimes is</i>) difficult to understand how to connect all the pieces. Thanks to Active Record now everything is way more clear and easy. 

One good example of this is my Sinatra Project. My app is about creating a list of your favorites Disneyland rides and having the option to add reviews to all the rides. At some point in the project, I had to create a list of all the rides by location. I thought the easiest way to do it was thru my Ride model using the location attribute to call the location and from there list the rides with the same location. To do this I created a <strong>self.location</strong> and <strong>self.by_land</strong> method and after that a <i>Helper</i> method to be able to use <strong>self.by_land</strong> inside my views (<i>sounds like a lot of work right?</i> <strong>IT WAS</strong>).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9g1vphg4gs6navrj5fr4.png)

Everything work and I was proud of my amazing methods but after having my 1:1 meeting with my coach she made me realize that yes, everything work just fine but the right way to do that was creating a location object and just use associations to create my list. After our call, I started working with that and I was just impressed with how easy it was to create my list just by using the associations and everything took me around 15 minutes. My ride <strong>belongs_to</strong> a location and my location <strong>has_many</strong> rides. Just as simple as that. I don’t need to build any other method because Active Record took care of that when I create my associations. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kxc52fq7zzp9x321oqch.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/taeowwudv5s8nybiuw25.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b91szrhnfkdbj6fmjshv.png)

Pretty amazing right? 

Active Record associations are a great tool and are here to make our lives easier. My new goal as a programmer is to identify when (<i>while I’m building my applications</i>) makes more sense to create an object and build associations between two Active Record models instead of just using attributes that at the end can probably get the job done but it would take way more time than necessary and as you know we, programmers, are lazy (<i>but in a good way</i>). 

Thank you for reading!