#sqlSucks
Explore tagged Tumblr posts
Text
Week 5, Day 1
Today was probably a big day for a lot of people that haven’t had any previous backend experience. Up until now we have been using Sinatra to create applications that don’t require data to persist in a database. For example our Movies app simply made use of the OMDB API to provide users with information about a particular movie. Unfortunately you cannot get very far in the real world with something as simple as this.
That is where ActiveRecord comes in. ActiveRecord essentially acts as a middleman between the application’s model(s) and the database. The beauty of active record is that it reduces the need to write your own SQL queries to read/write to the database. From the applications point of view, records are simply objects with properties (columns). Although i don’t mind writing SQL i find it amazing as to how this sort of architecture operates. Furthermore the SQL that ActiveRecord does produce is a lot more secure than i think i would be writing anyway.
After the introduction of ActiveRecord it was obvious that our movie app needed a bit of refactoring. The problem with our original app was that EVERY request from the user meant that an additional request (sometimes multiple) had to go out to OMDB to fetch our data - that is terrible. What i ended up doing was creating a simple caching method whereby my database would store movie data that had previously been fetched, eliminating the need for API calls in some cases. Many people dedicated database fields to certain bits of information about a movie. I took a different path. I didn’t want to restrict myself to certain bits of information from what was returned from OMDB. Instead i stored the hash (serialised using JSON) directly in my database. That made reading and writing data extremely easy in my opinion.
0 notes