W3D5 - ActiveRailsLite
Topics
ActiveRecord
Associations
Rails
Projects
ActiveRecordLite - build our own version of ActiveRecord using rspecs to check that everything's running correctly
Solo project
my_attr_accessor/readers
SQLObject class
::all
::find
#insert
#update
#save
Class Getter & Setter methods
::table_name
::table_name=
::columns - class method that lists all columns
::finalize! - automatically adds getter & setter methods
#attributes - use a hash to save all instant variables/attributes
#initialize - takes single params hash
::all - fetch all records from the DB
::parse_all
::find - return a single object with the given ID
#insert - builds and executes a SQL query to insert a row into the table
#update - updates a record's attributes
#save - calls #insert or #update
module Searchable - extend SQLObject with Searchable to use Searchable@where(params)
Associations
Associatable
AssocOptions
@class_name
#foreign_key
#primary_key
BelongsToOptions - extend AssocOptions
HasManyOptions - extend AssocOptions
#model_class - use String#constantize to go from class name --> class object
#table_name
Associatable#belongs_to - build a BelongsToOptions object
Associatable#has_many - build a HasManyOptions object
has_one_through - method that will combine two belongs_to associations
Learnings
It's much more fun to pair with a buddy!
It's takes more time to catch your own silly syntax errors...
ActiveSupport::Inflector - useful for changing plurality/singularity & case (snake_case/CamelCase) of strings & symbols to get the correct one when working in Rails
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html
There are so many video lectures this weekend!! And it does not pay off to try to get started on the work early, since App Academy uploaded new, shorter videos Saturday night, after I'd already watched the 3 hours of original video... >.<














