Skip to content

Instantly share code, notes, and snippets.

@stujo
Created February 24, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stujo/aaa3f8d9cff172354863 to your computer and use it in GitHub Desktop.
Save stujo/aaa3f8d9cff172354863 to your computer and use it in GitHub Desktop.
Advanced Active Record Notes

#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)')

  • https://github.com/rails/arel

  • arel: Band.arel_table.table_name

    • /work/projects/arel_notes/postings
    • psql
    • \connection postings_development

#Demo / Live-code Overview ##Demo Resources

#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

#Original Notes

use the associational interface

  1. 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.

  2. 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)

  3. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment