Phase-4-project
For my phase-4 project I decided to make a Pokemon card finder. I work at a trading card company and was interested in making a tool that would help someone keep track of their cards. Mostly me.
During phase-4 I learned how to handle instances of models better by using the great built in macros and instance variables that Active record provides. The only thing that is needed to create these nice associations between models is:
A. Add the macro belongs_to :parent to the child model and has_many :children to the child model.
B. Next you’ll add add a foreign key column in the child model for the parent model to associate with.
Great. These now add a whole bunch of methods to get different data based off whatever we’re looking for.
For example if we have a parent model, Authors, and a child mode books with a one to many association we can run the server and in our console type:
author=Author.first, author.books this will return a hash of the books associated to the instance of this author. These methods are very useful as shortcuts for getting data quick as they will work on any instance of the two models.
This is the epitome of DRY and therefore beautiful. The other thing I love is the the ease at which you can set up routes with route resources. Just adding in ‘resources :model’ is great and all but with great power… yah some times you want limited resources, for example if a user wasn’t logged in you wouldn’t want them to be able to post to a board. So we give them ‘resources :model, only:[:index, :show]’. This keeps this DRY but also safe.