I have been just over 10 months in Rails and ActiveRecord, but I just learned a tool I wish I would of known months ago.
I have seen scoped used in projects and even used them unknowingly, but I can say I understand them thanks to an explanation from a treehouse video.
The most likely use for this searching the table with shortcuts. If you find yourself searching in the console multiple times for the same records, it might be useful to set a scope in the model.
scope :premier, -> { where{"employees > ? , 50" )}
This premier scope can now be used People.premier.all in order to filter the search to only employees over the number 50. If you know SQL based commands well, you can change it and make different focuses. This might not as apparently useful now, but my recommendation to keep this tip in the back of your mind next time you find yourself all up in the console.
I plan to complete the Treehouse DB section to become a little more familiar with the SQL commands. I have attempted to jump in DB's in the past but fell asleep, due to it being so boring. Hopefully treehouse will excite me....
No comments:
Post a Comment