Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ActiveRecord Anti-Patterns

ActiveRecord Anti-Patterns

Ethan Gunderson

November 04, 2011
Tweet

Other Decks in Technology

Transcript

  1. 1. Write lock the table. 2. Make a copy of

    the table. 3. Make changes to the copy. 4. Promote copy to primary. 5. Repeat 1-4. Friday, November 4, 2011
  2. connection.execute(“ALTER TABLE `users` ADD COLUMN email varchar(255) DEFAULT NULL, ADD

    COLUMN status varchar(10) DEFAULT NULL”) Friday, November 4, 2011
  3. Iterate over the DB cursor and construct an array of

    rows. Initiate an array of ActiveRecord objects. Friday, November 4, 2011
  4. User 1 Inserts record User 2 Inserts record Checks for

    uniqueness Checks for uniqueness Friday, November 4, 2011
  5. class Order < ActiveRecord::Base validate :validate_cc_record def validate_cc_record return unless

    cc_id.changed? cc.user != user end end Friday, November 4, 2011
  6. Each unit should have only limited knowledge about other units:

    only units "closely" related to the current unit. Each unit should only talk to its friends; don't talk to strangers. Only talk to your immediate friends. Friday, November 4, 2011
  7. You can play with yourself. You can play with your

    toys. You can play with toys given to you. You can play with toys you’ve made yourself. http://c2.com/cgi/wiki?LawOfDemeter Friday, November 4, 2011
  8. class Order < ActiveRecord::Base delegate :address, :to => :user, :prefix

    => true, :allow_nil => true end Friday, November 4, 2011