#Advanced Active Record
#Bullet Points
group :test do
gem 'shoulda-matchers', require: false
end
-
Console logging - SQL to STDOUT
- ActiveRecord::Base.logger = Logger.new(STDOUT)
-
Relationships
- One to Many
- Has and Belongs To Many vs Has Many Through
- Non-Convention Named associations
- manager_id -> Employee
-
Custom Model Methods
- Simple Transformations
- Complex Lookups - Recursive
-
scopes
- ActiveRecord::Relation
- .to_sql / .explain
- .exists?
- .unscope / .except / .only / reorder / rewhere / none
- .readonly
- includes(:albums) eager load use as array
-
find / where / find_each(batch_size: 5000) / find_in_batches(include: :albums)
-
order, where(:attr =>[x,y,z]), not, limit, offset, select + group / having
-
Band.joins(:albums).select('bands.id','count(albums.id)','bands.name').group('bands.id').to_sql
-
Band.all.joins(:albums).select('bands.id','count(albums.id)','bands.name').group('bands.id').pluck('bands.id','bands.name','count(albums.id)')
-
arel: Band.arel_table.table_name
- /work/projects/arel_notes/postings
- psql
- \connection postings_development
#Demo / Live-code Overview ##Demo Resources
- active-record-example
- gem "sinatra-activerecord"
#Related Challenges
- TODO Challenge Name
- Common Sticking Point 1
- Common Sticking Point 2
##Demo Steps / Script
- TODO
- band.rb
- agent.try(:full_name)
- scope
- rake console
- Band.first.tracks.class
#Related Docs
- http://guides.rubyonrails.org/active_record_querying.html
- http://guides.rubyonrails.org/association_basics.html
- Rails Case Active Relation
- ActiveRecord Associations
- Sinatra How do I use ActiveRecord migrations?
- Create Table API
- Polymorphic Associations
#Original Notes
-
quickly walk through an example many-to-many association. Employees have employee_meetings, meetings have employee_meetings, employee_meetings belong to employees and meetings. models and migrations are already written.
-
use driver_code.rb for examples. some code already there, commented out. * basic access from collections (employee.meetings) * never reference a foreign key directly. (don't talk to employee_meetings!) walk through three ways to create. * chaining methods when necessary. where, clear (employee.meetings.where().clear) (clear removes associations)
-
use of complex, named associations when necessary. * employees have a manager_id column, which contains a foreign_key reference to the employee table. add manager_id column migration file, talk about adding instead of dropping databases. * add the manager association to the employee table. code is at the bottom of driver_code.rb. walk through some basic driver code. * add the underlings association (employees have many underlings). * talk about relying on the associational interface for queries. it can handle a lot! so what logic should be done in custom methods? two big categories:
- really simple attribute transforms, often for display. so something like a short name or a all capitals version of something.
- really complex logic, like recursively finding an employees boss' boss' boss.
Dev Bootcamp SF Phase 2