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

Active Record Demystified

Active Record Demystified

A brief overview of Active Record including some implementation details. Designed to explain some of the "magic" to beginners.

Anthony Lewis

March 07, 2012
Tweet

More Decks by Anthony Lewis

Other Decks in Programming

Transcript

  1. Active Record An object that wraps a row in a

    database table or view, encapsulates the database access, and adds domain logic on that data. Martin Fowler, Patterns of Enterprise Application Architecture
  2. Active Record in Rails • An implementation of the object-relational

    mapping (ORM) pattern of the same name • Automated mapping between classes and tables, attributes and columns • Associations between objects defined by simple class methods
  3. Posts id title body 1 Hello, World Welcome to my

    blog... 2 My Cat The cutest kitty in the... 3 Too Busy Sorry I haven’t posted...
  4. Posts id title body 1 Hello, World Welcome to my

    blog... 2 My Cat The cutest kitty in the... 3 Too Busy Sorry I haven’t posted... a post
  5. Posts id title body 1 Hello, World Welcome to my

    blog... 2 My Cat The cutest kitty in the... 3 Too Busy Sorry I haven’t posted... an attribute
  6. At The Console >> Post class Post < ActiveRecord::Base {

    :id => :integer, :title => :string, :body => :text }
  7. SQL

  8. The Database mysql> SHOW FIELDS FROM `posts`; +--------+--------------+------+-- | Field

    | Type | Null | +--------+--------------+------+-- | id | int(11) | NO | | title | varchar(255) | YES | | body | text | YES | +--------+--------------+------+--
  9. Attributes • Every ActiveRecord object stores the values for its

    attributes in a private variable called @attributes • ActiveRecord also adds methods for accessing each attribute such as title, title=, title?, etc.
  10. Create p = Post.new p.title = “My Cat” p.save #

    or simply Post.create(:title => “My Cat”)
  11. Read p = Post.find(2) p = Post.first p = Post.where(:title

    => “My Cat”) p = Post.order(‘title’).all p = Post.limit(5).all
  12. Update p = Post.find(2) p.title = “2nd” p.save # or

    simply p = Post.find(2) p.update_attributes(:title=>“2nd”)
  13. Method Missing def method_missing(meth, *args) if meth.to_s =~ /^find_by_(.+)$/ attr

    = $1.split(‘_and_’) find_by_attributes(attr, *args) else super end end
  14. Find By Attributes def find_by_attributes(attr, *args) conditions = Hash[attr.map {

    |a| [a, args[attr.index(a)]] }] where(conditions).first end
  15. Comments id author body post_id 1 Tom This is the

    worst... 1 2 Fred You are wrong... 1 3 Bob Lowest prices on... 1
  16. Comments id author body post_id 1 Tom This is the

    worst... 1 2 Fred You are wrong... 1 3 Bob Lowest prices on... 1 foreign key
  17. In Code class Post < ActiveRecord::Base has_many :comments end class

    Comment < ActiveRecord::Base belongs_to :post end
  18. Associations • An association is a connection between two models

    • Implemented with macro-style calls that add methods to a model • Streamline the code required to maintain the relationship between models
  19. In Code class Post < ActiveRecord::Base has_many :comments end class

    Comment < ActiveRecord::Base belongs_to :post end
  20. has_many Methods • comments • comments<< • comments.delete • comments=

    • comments.clear • comments.empty? • comments.size • comments.find • comments.where • comments.exists? • comments.build • comments.create
  21. Without Associations p = Post.find(1) c = Comment.new c.post_id =

    p.id c.author = “Tony” c.body = “A comment” c.save