- See more at: http://blogtimenow.com/blogging/automatically-redirect-blogger-blog-another-blog-website/#sthash.YsVbCxL0.dpuf Journey to Becoming a Web Developer: February 2014

Tuesday, February 11, 2014

Single Table Inheritance

I'm currently building an app that needs to have a base model and sub-models. I wasn't sure how to do that, so I did some research online and found STI or Single Table Inheritance.  The idea behind it is that the sub-models will inherit from the base model, so it will be able to use all of the attributes of the base model.  In the migration of the base model you must have a type field. This is how rails knows that other models will be inheriting from this model.  In the example below the singer and dancer models will inherit from the entertainer model, so now we can create a new entertainer object an be able to use the attributes associated with entertainer on that object.










Sunday, February 2, 2014

Lexical Scoping


In the example above I was able to find all of the even numbers in an array using lexical scoping and the .filter method.  Because I defined the getNumbers function inside of the parent function getEvenNumbers the child function getNumbers knows about numbersArray.  So when I call the .filter method it will iterate through each element in the array passed to numberArray then pass  it to the callback function getNumbers which checks to see if each element is divisible by 2.  .filter will return the results in a new array.