SlideShare a Scribd company logo
1 of 60
Download to read offline
Introduction to
 Active Record
    Evan ‘Rabble’ Henshaw-Plath
evan@protest.net - Yahoo! Brickhouse
 anarchogeek.com - testingrails.com
Active Record is a
    Design Pattern
An object that wraps a row in a database table
or view, encapsulates the database access, and
        adds domain logic on that data.
Active Record
          the Pattern
                 Person
            last_name
            first_name
            dependents_count
            insert
            update

            get_exemption
            is_flagged_for_audit?
            get_taxable_earnings?




Active Record uses the most obvious approach,
 putting data access logic in the domain object.
                - Martin Fowler
One Class Per Table
The Model Code                    The Database
class User < ActiveRecord::Base   CREATE TABLE `users` (
                                    `id` int(11) NOT NULL auto_increment,
end
                                    `login` varchar(255),
                                    `email` varchar(255),
                                    `crypted_password` varchar(40),
                                    `salt` varchar(40),
                                    `created_at` datetime default NULL,
                                    `updated_at` datetime default NULL,
                                    PRIMARY KEY (`id`)
                                  ) ENGINE=InnoDB;
One Object Per Row
There Are Other Ways
      To Do it
Active Record is
just one ‘Data Source
Architectural Pattern’

• Table Data Gateway
• Row Data Gateway
• Data Mapper
• The Anti-Patterns
Standard Active Record

• Direct mapping to the DB
 • Class to table
 • Object to row
• Simple, no relationship between objects
• Just a finder method with getters and setters
ActiveRecord
       the ruby library
Active Record is
a library built
for Ruby on Rails.

Makes CRUD Easy
Create
Read
Update
Delete
ActiveRecord
     the ruby library
I have never seen an Active Record
    implementation as complete
or as useful as rails. - Martin Fowler
Rails’ ActiveRecord
• DRY Conventions & Assumptions
• Validations
• Before and after filters
• Database Agnostic (mostly)
• Migrations
• Model relationships
 • has_many, belongs_to, etc...
What Active
Record Likes
• mapping class names to
  table names
• pluralized table names
• integer primary keys
• classname_id foreign keys
• simple schemas
• single table inheritance
Active Record
 Doesn’t Like
 • views
 • stored methods
 • foreign key constraints
 • cascading commits
 • split or clustered db’s
 • enums
The Basics
./app/models/user.rb                Loading a user
class User < ActiveRecord::Base
                                    >> user_obj = User.find(2)
end
                                    => #<User:0x352e8bc
                                    @attributes=
                                     {"salt"=>"d9ef...",
The SQL Log                           "updated_at"=>"2007-04-19 10:49:15",
                                      "crypted_password"=>"9c1...",
User Load (0.003175)                  "id"=>"2",
	    SELECT * FROM users              "remember_token"=>"a8d...",
	    WHERE (users.id = 2) LIMIT 1     "login"=>"rabble",
                                      "created_at"=>"2007-04-19 10:49:15",
                                      "email"=>"evan@protest.net"}>
The Find Method
Find is the primary method of Active Record

Examples:
	   User.find(23)
	   User.find(:first)
	   User.find(:all, :offset => 10, :limit => 10)
	   User.find(:all, :include => [:account, :friends])
	   User.find(:all, :conditions =>

   
 [“category in (?), categories, :limit => 50)
	   User.find(:first).articles
The Four Ways of Find
Find by id: This can either be a specific id (1), a list of ids (1, 5,
6), or an array of ids ([5, 6, 10]).

Find first: This will return the first record matched by the
options used.

Find all: This will return all the records matched by the options
used.

Indirectly: The find method is used for AR lookups via
associations.
Understanding Find

Model#find(:all, { parameters hash }

What Find Does:
	 * generates sql
	 * executes sql
	 * returns an enumerable (array like object)
	 * creates an AR model object for each row
Find with :conditions
:conditions - An SQL fragment like
	 "administrator = 1" or [ "user_name = ?", username ].

Student.find(:all, :conditions =>

 [‘first_name = ? and status = ?’ ‘rabble’, 1])

New Style (Edge Rails Only)
Student.find(:all, :conditions => {:first_name => “rabble”, :status => 1})

SQL Executed:
SELECT * FROM students WHERE (first_name = 'rabble' and status = 1);
Doing it Securely
class User < ActiveRecord::Base
  def self.authenticate_unsafely(user_name, password)
   find(:first, :conditions =>
        "user_name = '#{user_name}' AND password = '#{password}'")
  end

 def self.authenticate_safely(user_name, password)
  find(:first, :conditions =>
       [ "user_name = ? AND password = ?", user_name, password ])
 end

 # Edge Rails Only (Next Version of Rails)
 def self.authenticate_safely_simply(user_name, password)
  find(:first, :conditions =>
       { :user_name => user_name, :password => password })
 end
end
Order By

:order - An SQL fragment like "created_at DESC,
name".

Student.find(:all, :order => ‘updated_at DESC’)

SQL Executed:
SELECT * FROM users ORDER BY created_at;
Group By

:group - An attribute name by which the result
should be grouped. Uses the GROUP BY SQL-clause.

Student.find(:all, :group => ‘graduating_class’)

SQL Executed:
SELECT * FROM users GROUP BY graduating_class;
Limit & Offset
:limit - An integer determining the limit on the
number of rows that should be returned.

:offset- An integer determining the offset from
where the rows should be fetched. So at 5, it would
skip the first 4 rows.

Student.find(:all, :limit => 10, :offset => 0)

SQL Executed:
SELECT * FROM users LIMIT 0, 10;
Joins

:joins - An SQL fragment for additional joins like "LEFT
JOIN comments ON comments.post_id = id". (Rarely
needed).

Student.find(:all, :join =>
	   "LEFT JOIN comments ON comments.post_id = id")

SQL Executed:
SELECT * FROM users GROUP BY graduating_class;

Returns read only objects unless you say :readonly => false
Alternative Finds
find_by_sql
find_by_attribute_and_attribute2
find_or_create

Depreciated Find’s
find_first
find_all
find_on_conditions
Associations

The Four Primary Associations
belongs_to
has_one
has_many
has_and_belongs_to_many
class Project < ActiveRecord::Base
   belongs_to

 
 
 
 
 :portfolio
   has_one
 
 
 
 
 
 :project_manager
   has_many 
 
 
 
 
 
 :milestones
   has_and_belongs_to_many
 :categories
end
Associations

One to One
	 has_one & belongs_to
Many to One
	 has_many & belongs_to
Many to Many
	 has_and_belongs_to_many
	 has_many :through
One to One
Use has_one in the base, and belongs_to in the
associated model.

 class Employee < ActiveRecord::Base
  has_one :office
 end

 class Office < ActiveRecord::Base
  belongs_to :employee # foreign key - employee_id
 end
One To One Example
>> joe_employee = Employee.find_by_first_name('joe')
SELECT * FROM employees
	    WHERE (employees.`first_name` = 'joe') LIMIT 1
=> #<Employee:0x36beb14 @attributes={"id"=>"1", "first_name"=>"joe",
"last_name"=>"schmo", "created_at"=>"2007-04-21 09:08:59"}>

>> joes_office = joe_employee.office
SELECT * FROM offices WHERE (offices.employee_id = 1) LIMIT 1
=> #<Office:0x36bc06c @attributes={"employee_id"=>"1", "id"=>"1",
"created_at"=>"2007-04-21 09:11:44", "location"=>"A4302"}>

>> joes_office.employee
SELECT * FROM employees WHERE (employees.`id` = 1)
=> #<Employee:0x36b6ef0 @attributes={"id"=>"1", "first_name"=>"joe",
"last_name"=>"schmo", "created_at"=>"2007-04-21 09:08:59"}>
belongs_to
One to One Relationship.
Use belong to when the foreign key is in THIS table.

 •   Post#author (similar to Author.find(author_id) )
 •   Post#author=(author) (similar to post.author_id =
     author.id)
 •   Post#author? (similar to post.author == some_author)
 •   Post#author.nil?
 •   Post#build_author (similar to post.author =
     Author.new)
 •   Post#create_author (similar to post.author = Author;
     post.author.save;
Defining belongs_to
class Employee < ActiveRecord::Base
    belongs_to :firm, :foreign_key => "client_of"

    belongs_to :author, :class_name => "Person", :foreign_key => "author_id"

    belongs_to :valid_coupon, :class_name => "Coupon",


     
   :foreign_key => "coupon_id", :conditions => 'discounts > #{payments_count}'

    belongs_to :attachable, :polymorphic => true
end
has_one
One to One Relationship.
Use has_one when the foreign key is in the OTHER table.
 •   Account#beneficiary (similar to Beneficiary.find
     (:first, :conditions => "account_id = #{id}"))
 •   Account#beneficiary=(beneficiary) (similar to
     beneficiary.account_id = account.id; beneficiary.save)

 •   Account#beneficiary.nil?
 •   Account#build_beneficiary (similar to Beneficiary.new
     ("account_id" => id))

 •   Account#create_beneficiary (similar to b =
     Beneficiary.new("account_id" => id); b.save; b)
Defining has_one
class Employee < ActiveRecord::Base
 # destroys the associated credit card
 has_one :credit_card, :dependent => :destroy

 # updates the associated records foreign key value to null rather than destroying it
 has_one :credit_card, :dependent => :nullify

 has_one :last_comment, :class_name => "Comment", :order => "posted_on"

  has_one :project_manager, :class_name => "Person",
	    :conditions => "role = 'project_manager'"

 has_one :attachment, :as => :attachable

end
One to Many
One-to-many
Use has_many in the base, and belongs_to in
the associated model.

class Manager < ActiveRecord::Base
   has_many :employees
end

class Employee < ActiveRecord::Base
   belongs_to :manager # foreign key - manager_id
end
One to Many
>> benevolent_dictator = Manager.find(:first, :conditions => ['name = "DHH"'])
	 SELECT * FROM managers WHERE (name = "DHH") LIMIT 1
=> #<Manager:0x369b7b8 @attributes={"name"=>"DHH", "id"=>"1",
"created_at"=>"2007-04-21 09:59:24"}>

>> minions = benevolent_dictator.employees
	 SELECT * FROM employees WHERE (employees.manager_id = 1)
=> [#<Employee:0x36926a4 @attributes={"manager_id"=>"1", "id"=>"1",
"first_name"=>"joe", "last_name"=>"schmo", "created_at"=>"2007-04-21
09:08:59"}>,
	 #<Employee:0x36925f0 @attributes={"manager_id"=>"1", "id"=>"2",
"first_name"=>"funky", "last_name"=>"monkey", "created_at"=>"2007-04-21
09:58:20"}>]
has_many
                  Augmenting the Model
•   Firm#clients (similar to Clients.find :all, :conditions =>
    "firm_id = #{id}")
•   Firm#clients<<
•   Firm#clients.delete
•   Firm#client_ids
•   Firm#client_ids=
•   Firm#clients=
has_many
•   Firm#client.clear
•   Firm#clients.empty? (similar to firm.clients.size
    == 0)
•   Firm#clients.size (similar to Client.count
    "firm_id = #{id}")
•   Firm#clients.find (similar to Client.find(id, :conditions
    => "firm_id = #{id}"))
•   Firm#clients.build (similar to Client.new
    ("firm_id" => id))
•   Firm#clients.create (similar to c = Client.new
    ("firm_id" => id); c.save; c)
has_many examples
class Employee < ActiveRecord::Base
 has_many :comments, :order => "posted_on"
 has_many :comments, :include => :author
 has_many :people, :class_name => "Person", :conditions => "deleted = 0", :order => "name"
 has_many :tracks, :order => "position", :dependent => :destroy
 has_many :comments, :dependent => :nullify
 has_many :tags, :as => :taggable
 has_many :subscribers, :through => :subscriptions, :source => :user
 has_many :subscribers, :class_name => "Person", :finder_sql =>
   'SELECT DISTINCT people.* ' +
   'FROM people p, post_subscriptions ps ' +
   'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' +
   'ORDER BY p.first_name'
end
Many to Many
    Simple Joiner Table
has_and_belongs_to_many

      Joiner Model
   has_many :through
has_and_belongs_to_many
  The Simple Joiner Table Way
has_and_belongs_to_many
neglected
    by
rails-core
has_and_belongs_to_many
                Augmenting the Model
  •   Developer#projects
  •   Developer#projects<<
  •   Developer#projects.delete
  •   Developer#projects=
  •   Developer#projects_ids
  •   Developer#projects_ids=
  •   Developer#clear
has_and_belongs_to_many

 •   Developer#projects.empty?
 •   Developer#projects.size
 •   Developer#projects.find(id) # Also find(:first / :all)
 •   Developer#projects.build #(similar to Project.new
     ("project_id" => id))
 •   Developer#projects.create (similar to c =
     Project.new("project_id" => id); c.save; c)
habtm example
create_table :developers do |t|
 t.column :name, :string
 t.column :created_at, :datetime
end

create_table :projects do |t|
 t.column :name, :string
 t.column :created_at, :datetime
end

create_table(:developers_projects, :id => false) do |t|
 t.column :developer_id, :integer
 t.column :project_id, :integer
end
habtm example
>> d = Developer.find(1)

    SELECT * FROM developers WHERE (developers.`id` = 1)
=> #<Developer:0x32bc7dc @attributes={"name"=>"rabble", "id"=>"1",
"created_at"=>nil}>

>> d.projects

     SELECT * FROM projects INNER JOIN developers_projects ON projects.id =
developers_projects.project_id WHERE (developers_projects.developer_id = 1 )
=> [#<Project:0x3257cc4
  @attributes=
  {"name"=>"ragi",
   "project_id"=>"1",
   "id"=>"1",
   "developer_id"=>"1",
   "created_at"=>nil}>,
 #<Project:0x3257c10
  @attributes=
  {"name"=>"acts_as_autenticated",
   "project_id"=>"3",
   "id"=>"3",
   "developer_id"=>"1",
   "created_at"=>nil}>]
has_many :through

    DHH’s
 One True Way
of Many to Many
has_many :through

Full Joiner Model
has_many :through
  class Appearance < ActiveRecord::Base
   belongs_to :dancer
   belongs_to :movie
  end

  class Dancer < ActiveRecord::Base
   has_many :appearances, :dependent => true
   has_many :movies, :through => :appearances
  end

  class Movie < ActiveRecord::Base
   has_many :appearances, :dependent => true
   has_many :dancers, :through => :appearances
  end
Validations
class User < ActiveRecord::Base

 validates_confirmation_of :login, :password

 validates_confirmation_of :email,

 
 :message => "should match confirmation"

 validates_format_of :email,

 
 :with => /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})/i,

 
 :on = :create
end
Validations
• Keeping Data Clean
• In object validation of fields, calculated
  validations
• Instead of key constraints
• The database is for storage, the model is for
  the business logic
• Kinds of validations, custom validations, etc...
But Wait?

• Aren’t format, presence, relationship
  validations supposed to be the database’s
  job?
• Traditionally, yes.
• ActiveRecord does constraints in the
  model, not the database
But Why?

• Validations  Constraints are Business Logic
• Business logic should be in the model
• It makes things easy
• End users can get useful error messages
• Makes the postback pattern work well
Data Integrity?
• It’s still possible to do constraints in the db
• But it’s not as necessary
• Validations are constraints which make
  sense in terms of functionality of the app
• The rails ways is to just use validations
• Most DBA’s insist on foreign_key
  constraints
What AR Returns?
• Enumerable Objects (kind of like arrays)
• Preselects and instantiates objects
• Nifty methods: to_yaml, to_xml, to_json
Output Formats
ruby - inspect                                to_yaml
#Employee:0x36926a4                          --- !ruby/object:Employee
@attributes=                                  attributes:
 {manager_id=1,                            manager_id: 1
  id=1,                                    id: 1
  first_name=joe,                           first_name: joe
  last_name=schmo,                         last_name: schmo
  created_at=2007-04-21 09:08:59}         created_at: 2007-04-21 09:08:59


to_xml                                        to_json
?xml version=1.0 encoding=UTF-8?
                                              {attributes:
employee
                                              	    {manager_id: 1,
  created-at
                                              	    id: 1,
type=datetime2007-04-21T09:08:59-07:00/
created-at                                   	    first_name: joe,
  first-namejoe/first-name
                                              	    last_name: schmo,
  id type=integer1/id
                                              	    created_at: 2007-04-21 09:08:59}}
  last-nameschmo/last-name
  manager-id type=integer1/manager-id
/employee
Before  After
                            Callbacks
                                                             * (-) save
class Subscription  ActiveRecord::Base
                                                             * (-) valid?
  before_create :record_signup
                                                             * (1) before_validation
  private                                                    * (2) before_validation_on_create
   def record_signup                                         * (-) validate
     self.signed_up_on = Date.today
                                                             * (-) validate_on_create
   end
                                                             * (3) after_validation
 end
                                                             * (4) after_validation_on_create
                                                             * (5) before_save
 class Firm  ActiveRecord::Base
  # Destroys the associated clients and                      * (6) before_create
  #people when the firm is destroyed                          * (-) create
  before_destroy {                                           * (7) after_create

 |record| Person.destroy_all firm_id = #{record.id} }
                                                             * (8) after_save
  before_destroy {

 |record| Client.destroy_all client_of = #{record.id} }
 end
Security
Special Fields
* created_at     * #{table_name}_count
                 * position
* created_on
                 * parent_id
* updated_at
                 * lft
* updated_on
                 * rgt
* lock_version
                 * quote
* type
                 * template
* id
Active Record Tricks
Drink the Kool aid?
Flickr Photos Used:
http://flickr.com/photos/brraveheart/114402291/
                                                    http://flickr.com/photos/ryangreenberg/57722319/
http://flickr.com/photos/bright/253175260/
                                                    http://flickr.com/photos/benandliz/11065337/
http://flickr.com/photos/good_day/63617697/
                                                    http://flickr.com/photos/gaspi/12944421/
http://flickr.com/photos/rickharris/416150393/
                                                    http://flickr.com/photos/thomashawk/221827536/
http://flickr.com/photos/babasteve/3322247/
                                                    http://flickr.com/photos/brianboulos/7707518/
http://flickr.com/photos/olivander/28058685/
                                                    http://flickr.com/photos/ross/28330560/
http://flickr.com/photos/brraveheart/44052308/
                                                    http://flickr.com/photos/emdot/45249090/
http://flickr.com/photos/ednothing/142393509/
                                                    http://flickr.com/photos/farhang/428136695/
http://flickr.com/photos/alltheaces/87505524/
                                                    http://flickr.com/photos/belljar/67877047/
http://flickr.com/photos/alfr3do/7436142/
                                                    http://flickr.com/photos/pulpolux/34545782/
http://flickr.com/photos/gdominici/57975123/
                                                    http://flickr.com/photos/monkeyc/107979135/
http://flickr.com/photos/josefstuefer/72512671/
                                                    http://flickr.com/photos/pedrosimoes7/449314732/
http://flickr.com/photos/uqbar/105440294/
                                                    http://flickr.com/photos/dincordero/405452471/
http://flickr.com/photos/auntiep/17135231/
                                                    http://flickr.com/photos/andidfl/203883534/
http://flickr.com/photos/einsame_spitze/406992131/
                                                    http://flickr.com/photos/ivanomak/434387836/
http://flickr.com/photos/beija-flor/63758047/
                                                    http://flickr.com/photos/nrvica/23858419/
http://flickr.com/photos/amerune/174617912/
                                                    http://flickr.com/photos/thespeak/137012632/
http://flickr.com/photos/hungry_i/47938311/
http://flickr.com/photos/santos/13952912/
http://flickr.com/photos/supermietzi/179962496/
http://flickr.com/photos/traveller2020/206931940/
http://flickr.com/photos/ko_an/318906221/
Questions?

 Introduction to
  Active Record
    Evan ‘Rabble’ Henshaw-Plath
evan@protest.net - Yahoo! Brickhouse
 anarchogeek.com - testingrails.com

More Related Content

What's hot

ORM2Pwn: Exploiting injections in Hibernate ORM
ORM2Pwn: Exploiting injections in Hibernate ORMORM2Pwn: Exploiting injections in Hibernate ORM
ORM2Pwn: Exploiting injections in Hibernate ORMMikhail Egorov
 
Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016Colin O'Dell
 
Hacking Your Way To Better Security
Hacking Your Way To Better SecurityHacking Your Way To Better Security
Hacking Your Way To Better SecurityColin O'Dell
 
Hacking Your Way to Better Security - PHP South Africa 2016
Hacking Your Way to Better Security - PHP South Africa 2016Hacking Your Way to Better Security - PHP South Africa 2016
Hacking Your Way to Better Security - PHP South Africa 2016Colin O'Dell
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginningAnis Ahmad
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue AdventureAllegient
 
Php Security By Mugdha And Anish
Php Security By Mugdha And AnishPhp Security By Mugdha And Anish
Php Security By Mugdha And AnishOSSCube
 
Form demoinplaywithmysql
Form demoinplaywithmysqlForm demoinplaywithmysql
Form demoinplaywithmysqlKnoldus Inc.
 
Dig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup CairoDig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup CairoMohamed Mosaad
 
Mule esb – connecting to ms sql db
Mule esb – connecting to ms sql dbMule esb – connecting to ms sql db
Mule esb – connecting to ms sql dbGunjan Deshmukh
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateKiev ALT.NET
 
Filtering data with D2W
Filtering data with D2W Filtering data with D2W
Filtering data with D2W WO Community
 
Hacking Your Way to Better Security - ZendCon 2016
Hacking Your Way to Better Security - ZendCon 2016Hacking Your Way to Better Security - ZendCon 2016
Hacking Your Way to Better Security - ZendCon 2016Colin O'Dell
 
UKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseUKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseMarco Gralike
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with SolrErik Hatcher
 

What's hot (19)

ORM2Pwn: Exploiting injections in Hibernate ORM
ORM2Pwn: Exploiting injections in Hibernate ORMORM2Pwn: Exploiting injections in Hibernate ORM
ORM2Pwn: Exploiting injections in Hibernate ORM
 
Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016
 
Hacking Your Way To Better Security
Hacking Your Way To Better SecurityHacking Your Way To Better Security
Hacking Your Way To Better Security
 
Hacking Your Way to Better Security - PHP South Africa 2016
Hacking Your Way to Better Security - PHP South Africa 2016Hacking Your Way to Better Security - PHP South Africa 2016
Hacking Your Way to Better Security - PHP South Africa 2016
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
 
ORM Injection
ORM InjectionORM Injection
ORM Injection
 
Advanced Django
Advanced DjangoAdvanced Django
Advanced Django
 
Php Security By Mugdha And Anish
Php Security By Mugdha And AnishPhp Security By Mugdha And Anish
Php Security By Mugdha And Anish
 
Form demoinplaywithmysql
Form demoinplaywithmysqlForm demoinplaywithmysql
Form demoinplaywithmysql
 
Dig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup CairoDig Deeper into WordPress - WD Meetup Cairo
Dig Deeper into WordPress - WD Meetup Cairo
 
Mule esb – connecting to ms sql db
Mule esb – connecting to ms sql dbMule esb – connecting to ms sql db
Mule esb – connecting to ms sql db
 
SQLAlchemy Seminar
SQLAlchemy SeminarSQLAlchemy Seminar
SQLAlchemy Seminar
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicate
 
JavaScript JQUERY AJAX
JavaScript JQUERY AJAXJavaScript JQUERY AJAX
JavaScript JQUERY AJAX
 
Filtering data with D2W
Filtering data with D2W Filtering data with D2W
Filtering data with D2W
 
Hacking Your Way to Better Security - ZendCon 2016
Hacking Your Way to Better Security - ZendCon 2016Hacking Your Way to Better Security - ZendCon 2016
Hacking Your Way to Better Security - ZendCon 2016
 
UKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseUKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the Database
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
 

Viewers also liked

Ruby on Rails Vs. ASP.NET MVC
Ruby on Rails Vs. ASP.NET MVCRuby on Rails Vs. ASP.NET MVC
Ruby on Rails Vs. ASP.NET MVCShay Friedman
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentationadamcookeuk
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails PresentationJoost Hietbrink
 
Active Record Internals - Medellin.rb
Active Record Internals - Medellin.rbActive Record Internals - Medellin.rb
Active Record Internals - Medellin.rbOscar Rendon
 

Viewers also liked (6)

Ruby on Rails Vs. ASP.NET MVC
Ruby on Rails Vs. ASP.NET MVCRuby on Rails Vs. ASP.NET MVC
Ruby on Rails Vs. ASP.NET MVC
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Rails Best Practices
Rails Best PracticesRails Best Practices
Rails Best Practices
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
reveal.js 3.0.0
reveal.js 3.0.0reveal.js 3.0.0
reveal.js 3.0.0
 
Active Record Internals - Medellin.rb
Active Record Internals - Medellin.rbActive Record Internals - Medellin.rb
Active Record Internals - Medellin.rb
 

Similar to Introduction to Active Record design pattern

Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Yasuko Ohba
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Railsrstankov
 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperTame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperGiordano Scalzo
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Prxibbar
 
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013Joao Lucas Santana
 
Playing With (B)Sqli
Playing With (B)SqliPlaying With (B)Sqli
Playing With (B)SqliChema Alonso
 
Building Web-API without Rails, Registration or SMS
Building Web-API without Rails, Registration or SMSBuilding Web-API without Rails, Registration or SMS
Building Web-API without Rails, Registration or SMSPivorak MeetUp
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsMike Subelsky
 
Object Relational Mapping in PHP
Object Relational Mapping in PHPObject Relational Mapping in PHP
Object Relational Mapping in PHPRob Knight
 
Active Record Inheritance in Rails
Active Record Inheritance in RailsActive Record Inheritance in Rails
Active Record Inheritance in RailsSandip Ransing
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::ManagerJay Shirley
 
JSON SQL Injection and the Lessons Learned
JSON SQL Injection and the Lessons LearnedJSON SQL Injection and the Lessons Learned
JSON SQL Injection and the Lessons LearnedKazuho Oku
 
Ruby on Rails For .Net Programmers
Ruby on Rails For .Net ProgrammersRuby on Rails For .Net Programmers
Ruby on Rails For .Net Programmersdaveverwer
 
Euruko 2009 - DataObjects
Euruko 2009 - DataObjectsEuruko 2009 - DataObjects
Euruko 2009 - DataObjectsDirkjan Bussink
 

Similar to Introduction to Active Record design pattern (20)

Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
DataMapper
DataMapperDataMapper
DataMapper
 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperTame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
 
Why ruby
Why rubyWhy ruby
Why ruby
 
SOLID Ruby, SOLID Rails
SOLID Ruby, SOLID RailsSOLID Ruby, SOLID Rails
SOLID Ruby, SOLID Rails
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Pr
 
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
 
Playing With (B)Sqli
Playing With (B)SqliPlaying With (B)Sqli
Playing With (B)Sqli
 
Building Web-API without Rails, Registration or SMS
Building Web-API without Rails, Registration or SMSBuilding Web-API without Rails, Registration or SMS
Building Web-API without Rails, Registration or SMS
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
Php summary
Php summaryPhp summary
Php summary
 
Object Relational Mapping in PHP
Object Relational Mapping in PHPObject Relational Mapping in PHP
Object Relational Mapping in PHP
 
SOLID Ruby SOLID Rails
SOLID Ruby SOLID RailsSOLID Ruby SOLID Rails
SOLID Ruby SOLID Rails
 
Active Record Inheritance in Rails
Active Record Inheritance in RailsActive Record Inheritance in Rails
Active Record Inheritance in Rails
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::Manager
 
JSON SQL Injection and the Lessons Learned
JSON SQL Injection and the Lessons LearnedJSON SQL Injection and the Lessons Learned
JSON SQL Injection and the Lessons Learned
 
Ruby on Rails For .Net Programmers
Ruby on Rails For .Net ProgrammersRuby on Rails For .Net Programmers
Ruby on Rails For .Net Programmers
 
Migrating legacy data
Migrating legacy dataMigrating legacy data
Migrating legacy data
 
Euruko 2009 - DataObjects
Euruko 2009 - DataObjectsEuruko 2009 - DataObjects
Euruko 2009 - DataObjects
 

More from Rabble .

CoDesign CMS.362/CMS.862 MIT Evolution of Product Design
CoDesign CMS.362/CMS.862 MIT Evolution of Product DesignCoDesign CMS.362/CMS.862 MIT Evolution of Product Design
CoDesign CMS.362/CMS.862 MIT Evolution of Product DesignRabble .
 
Building a Hacker Culture in Uruguay - OSCON 2011
Building a Hacker Culture in Uruguay - OSCON 2011Building a Hacker Culture in Uruguay - OSCON 2011
Building a Hacker Culture in Uruguay - OSCON 2011Rabble .
 
La Historia Secreta de Twitter & El Modelo de los Lean Startups
La Historia Secreta de Twitter & El Modelo de los  Lean StartupsLa Historia Secreta de Twitter & El Modelo de los  Lean Startups
La Historia Secreta de Twitter & El Modelo de los Lean StartupsRabble .
 
Ruby Culture
Ruby CultureRuby Culture
Ruby CultureRabble .
 
Finding the Middle Way of Testing
Finding the Middle Way of TestingFinding the Middle Way of Testing
Finding the Middle Way of TestingRabble .
 
Hacking Frequent Flyer Programs
Hacking Frequent Flyer ProgramsHacking Frequent Flyer Programs
Hacking Frequent Flyer ProgramsRabble .
 
Desde Software Libre Hacia Datos Abiertos
Desde Software Libre Hacia Datos AbiertosDesde Software Libre Hacia Datos Abiertos
Desde Software Libre Hacia Datos AbiertosRabble .
 
Sobre Hombros de Gigantes: Desarrollo de tecnología y la historia secreto de...
Sobre Hombros de Gigantes: Desarrollo de tecnología y  la historia secreto de...Sobre Hombros de Gigantes: Desarrollo de tecnología y  la historia secreto de...
Sobre Hombros de Gigantes: Desarrollo de tecnología y la historia secreto de...Rabble .
 
Beyond Testing: Specs and Behavior Driven Development
Beyond Testing: Specs and Behavior  Driven DevelopmentBeyond Testing: Specs and Behavior  Driven Development
Beyond Testing: Specs and Behavior Driven DevelopmentRabble .
 
Beyond REST? Building Data Services with XMPP PubSub
Beyond REST? Building Data Services with XMPP PubSubBeyond REST? Building Data Services with XMPP PubSub
Beyond REST? Building Data Services with XMPP PubSubRabble .
 
Liberating Location - Fire Eagle - Ecomm 2008
Liberating Location - Fire Eagle - Ecomm 2008Liberating Location - Fire Eagle - Ecomm 2008
Liberating Location - Fire Eagle - Ecomm 2008Rabble .
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails AppsRabble .
 
Phone Communities and Activism Showcase
Phone Communities and Activism ShowcasePhone Communities and Activism Showcase
Phone Communities and Activism ShowcaseRabble .
 

More from Rabble . (13)

CoDesign CMS.362/CMS.862 MIT Evolution of Product Design
CoDesign CMS.362/CMS.862 MIT Evolution of Product DesignCoDesign CMS.362/CMS.862 MIT Evolution of Product Design
CoDesign CMS.362/CMS.862 MIT Evolution of Product Design
 
Building a Hacker Culture in Uruguay - OSCON 2011
Building a Hacker Culture in Uruguay - OSCON 2011Building a Hacker Culture in Uruguay - OSCON 2011
Building a Hacker Culture in Uruguay - OSCON 2011
 
La Historia Secreta de Twitter & El Modelo de los Lean Startups
La Historia Secreta de Twitter & El Modelo de los  Lean StartupsLa Historia Secreta de Twitter & El Modelo de los  Lean Startups
La Historia Secreta de Twitter & El Modelo de los Lean Startups
 
Ruby Culture
Ruby CultureRuby Culture
Ruby Culture
 
Finding the Middle Way of Testing
Finding the Middle Way of TestingFinding the Middle Way of Testing
Finding the Middle Way of Testing
 
Hacking Frequent Flyer Programs
Hacking Frequent Flyer ProgramsHacking Frequent Flyer Programs
Hacking Frequent Flyer Programs
 
Desde Software Libre Hacia Datos Abiertos
Desde Software Libre Hacia Datos AbiertosDesde Software Libre Hacia Datos Abiertos
Desde Software Libre Hacia Datos Abiertos
 
Sobre Hombros de Gigantes: Desarrollo de tecnología y la historia secreto de...
Sobre Hombros de Gigantes: Desarrollo de tecnología y  la historia secreto de...Sobre Hombros de Gigantes: Desarrollo de tecnología y  la historia secreto de...
Sobre Hombros de Gigantes: Desarrollo de tecnología y la historia secreto de...
 
Beyond Testing: Specs and Behavior Driven Development
Beyond Testing: Specs and Behavior  Driven DevelopmentBeyond Testing: Specs and Behavior  Driven Development
Beyond Testing: Specs and Behavior Driven Development
 
Beyond REST? Building Data Services with XMPP PubSub
Beyond REST? Building Data Services with XMPP PubSubBeyond REST? Building Data Services with XMPP PubSub
Beyond REST? Building Data Services with XMPP PubSub
 
Liberating Location - Fire Eagle - Ecomm 2008
Liberating Location - Fire Eagle - Ecomm 2008Liberating Location - Fire Eagle - Ecomm 2008
Liberating Location - Fire Eagle - Ecomm 2008
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
 
Phone Communities and Activism Showcase
Phone Communities and Activism ShowcasePhone Communities and Activism Showcase
Phone Communities and Activism Showcase
 

Recently uploaded

(办理原版一样)QUT毕业证昆士兰科技大学毕业证学位证留信学历认证成绩单补办
(办理原版一样)QUT毕业证昆士兰科技大学毕业证学位证留信学历认证成绩单补办(办理原版一样)QUT毕业证昆士兰科技大学毕业证学位证留信学历认证成绩单补办
(办理原版一样)QUT毕业证昆士兰科技大学毕业证学位证留信学历认证成绩单补办fqiuho152
 
Tenets of Physiocracy History of Economic
Tenets of Physiocracy History of EconomicTenets of Physiocracy History of Economic
Tenets of Physiocracy History of Economiccinemoviesu
 
Stock Market Brief Deck FOR 4/17 video.pdf
Stock Market Brief Deck FOR 4/17 video.pdfStock Market Brief Deck FOR 4/17 video.pdf
Stock Market Brief Deck FOR 4/17 video.pdfMichael Silva
 
House of Commons ; CDC schemes overview document
House of Commons ; CDC schemes overview documentHouse of Commons ; CDC schemes overview document
House of Commons ; CDC schemes overview documentHenry Tapper
 
NO1 WorldWide online istikhara for love marriage vashikaran specialist love p...
NO1 WorldWide online istikhara for love marriage vashikaran specialist love p...NO1 WorldWide online istikhara for love marriage vashikaran specialist love p...
NO1 WorldWide online istikhara for love marriage vashikaran specialist love p...Amil Baba Dawood bangali
 
Quantitative Analysis of Retail Sector Companies
Quantitative Analysis of Retail Sector CompaniesQuantitative Analysis of Retail Sector Companies
Quantitative Analysis of Retail Sector Companiesprashantbhati354
 
(中央兰开夏大学毕业证学位证成绩单-案例)
(中央兰开夏大学毕业证学位证成绩单-案例)(中央兰开夏大学毕业证学位证成绩单-案例)
(中央兰开夏大学毕业证学位证成绩单-案例)twfkn8xj
 
The Core Functions of the Bangko Sentral ng Pilipinas
The Core Functions of the Bangko Sentral ng PilipinasThe Core Functions of the Bangko Sentral ng Pilipinas
The Core Functions of the Bangko Sentral ng PilipinasCherylouCamus
 
fca-bsps-decision-letter-redacted (1).pdf
fca-bsps-decision-letter-redacted (1).pdffca-bsps-decision-letter-redacted (1).pdf
fca-bsps-decision-letter-redacted (1).pdfHenry Tapper
 
The Triple Threat | Article on Global Resession | Harsh Kumar
The Triple Threat | Article on Global Resession | Harsh KumarThe Triple Threat | Article on Global Resession | Harsh Kumar
The Triple Threat | Article on Global Resession | Harsh KumarHarsh Kumar
 
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...Amil Baba Dawood bangali
 
NO1 WorldWide Genuine vashikaran specialist Vashikaran baba near Lahore Vashi...
NO1 WorldWide Genuine vashikaran specialist Vashikaran baba near Lahore Vashi...NO1 WorldWide Genuine vashikaran specialist Vashikaran baba near Lahore Vashi...
NO1 WorldWide Genuine vashikaran specialist Vashikaran baba near Lahore Vashi...Amil baba
 
Bladex 1Q24 Earning Results Presentation
Bladex 1Q24 Earning Results PresentationBladex 1Q24 Earning Results Presentation
Bladex 1Q24 Earning Results PresentationBladex
 
SBP-Market-Operations and market managment
SBP-Market-Operations and market managmentSBP-Market-Operations and market managment
SBP-Market-Operations and market managmentfactical
 
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...Henry Tapper
 
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...Amil baba
 
The AES Investment Code - the go-to counsel for the most well-informed, wise...
The AES Investment Code -  the go-to counsel for the most well-informed, wise...The AES Investment Code -  the go-to counsel for the most well-informed, wise...
The AES Investment Code - the go-to counsel for the most well-informed, wise...AES International
 
Stock Market Brief Deck for "this does not happen often".pdf
Stock Market Brief Deck for "this does not happen often".pdfStock Market Brief Deck for "this does not happen often".pdf
Stock Market Brief Deck for "this does not happen often".pdfMichael Silva
 
Classical Theory of Macroeconomics by Adam Smith
Classical Theory of Macroeconomics by Adam SmithClassical Theory of Macroeconomics by Adam Smith
Classical Theory of Macroeconomics by Adam SmithAdamYassin2
 

Recently uploaded (20)

(办理原版一样)QUT毕业证昆士兰科技大学毕业证学位证留信学历认证成绩单补办
(办理原版一样)QUT毕业证昆士兰科技大学毕业证学位证留信学历认证成绩单补办(办理原版一样)QUT毕业证昆士兰科技大学毕业证学位证留信学历认证成绩单补办
(办理原版一样)QUT毕业证昆士兰科技大学毕业证学位证留信学历认证成绩单补办
 
Tenets of Physiocracy History of Economic
Tenets of Physiocracy History of EconomicTenets of Physiocracy History of Economic
Tenets of Physiocracy History of Economic
 
Stock Market Brief Deck FOR 4/17 video.pdf
Stock Market Brief Deck FOR 4/17 video.pdfStock Market Brief Deck FOR 4/17 video.pdf
Stock Market Brief Deck FOR 4/17 video.pdf
 
House of Commons ; CDC schemes overview document
House of Commons ; CDC schemes overview documentHouse of Commons ; CDC schemes overview document
House of Commons ; CDC schemes overview document
 
NO1 WorldWide online istikhara for love marriage vashikaran specialist love p...
NO1 WorldWide online istikhara for love marriage vashikaran specialist love p...NO1 WorldWide online istikhara for love marriage vashikaran specialist love p...
NO1 WorldWide online istikhara for love marriage vashikaran specialist love p...
 
Quantitative Analysis of Retail Sector Companies
Quantitative Analysis of Retail Sector CompaniesQuantitative Analysis of Retail Sector Companies
Quantitative Analysis of Retail Sector Companies
 
(中央兰开夏大学毕业证学位证成绩单-案例)
(中央兰开夏大学毕业证学位证成绩单-案例)(中央兰开夏大学毕业证学位证成绩单-案例)
(中央兰开夏大学毕业证学位证成绩单-案例)
 
The Core Functions of the Bangko Sentral ng Pilipinas
The Core Functions of the Bangko Sentral ng PilipinasThe Core Functions of the Bangko Sentral ng Pilipinas
The Core Functions of the Bangko Sentral ng Pilipinas
 
Q1 2024 Newsletter | Financial Synergies Wealth Advisors
Q1 2024 Newsletter | Financial Synergies Wealth AdvisorsQ1 2024 Newsletter | Financial Synergies Wealth Advisors
Q1 2024 Newsletter | Financial Synergies Wealth Advisors
 
fca-bsps-decision-letter-redacted (1).pdf
fca-bsps-decision-letter-redacted (1).pdffca-bsps-decision-letter-redacted (1).pdf
fca-bsps-decision-letter-redacted (1).pdf
 
The Triple Threat | Article on Global Resession | Harsh Kumar
The Triple Threat | Article on Global Resession | Harsh KumarThe Triple Threat | Article on Global Resession | Harsh Kumar
The Triple Threat | Article on Global Resession | Harsh Kumar
 
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
 
NO1 WorldWide Genuine vashikaran specialist Vashikaran baba near Lahore Vashi...
NO1 WorldWide Genuine vashikaran specialist Vashikaran baba near Lahore Vashi...NO1 WorldWide Genuine vashikaran specialist Vashikaran baba near Lahore Vashi...
NO1 WorldWide Genuine vashikaran specialist Vashikaran baba near Lahore Vashi...
 
Bladex 1Q24 Earning Results Presentation
Bladex 1Q24 Earning Results PresentationBladex 1Q24 Earning Results Presentation
Bladex 1Q24 Earning Results Presentation
 
SBP-Market-Operations and market managment
SBP-Market-Operations and market managmentSBP-Market-Operations and market managment
SBP-Market-Operations and market managment
 
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
 
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
 
The AES Investment Code - the go-to counsel for the most well-informed, wise...
The AES Investment Code -  the go-to counsel for the most well-informed, wise...The AES Investment Code -  the go-to counsel for the most well-informed, wise...
The AES Investment Code - the go-to counsel for the most well-informed, wise...
 
Stock Market Brief Deck for "this does not happen often".pdf
Stock Market Brief Deck for "this does not happen often".pdfStock Market Brief Deck for "this does not happen often".pdf
Stock Market Brief Deck for "this does not happen often".pdf
 
Classical Theory of Macroeconomics by Adam Smith
Classical Theory of Macroeconomics by Adam SmithClassical Theory of Macroeconomics by Adam Smith
Classical Theory of Macroeconomics by Adam Smith
 

Introduction to Active Record design pattern

  • 1. Introduction to Active Record Evan ‘Rabble’ Henshaw-Plath evan@protest.net - Yahoo! Brickhouse anarchogeek.com - testingrails.com
  • 2. Active Record is a Design Pattern An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.
  • 3. Active Record the Pattern Person last_name first_name dependents_count insert update get_exemption is_flagged_for_audit? get_taxable_earnings? Active Record uses the most obvious approach, putting data access logic in the domain object. - Martin Fowler
  • 4. One Class Per Table The Model Code The Database class User < ActiveRecord::Base CREATE TABLE `users` ( `id` int(11) NOT NULL auto_increment, end `login` varchar(255), `email` varchar(255), `crypted_password` varchar(40), `salt` varchar(40), `created_at` datetime default NULL, `updated_at` datetime default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;
  • 6. There Are Other Ways To Do it Active Record is just one ‘Data Source Architectural Pattern’ • Table Data Gateway • Row Data Gateway • Data Mapper • The Anti-Patterns
  • 7. Standard Active Record • Direct mapping to the DB • Class to table • Object to row • Simple, no relationship between objects • Just a finder method with getters and setters
  • 8. ActiveRecord the ruby library Active Record is a library built for Ruby on Rails. Makes CRUD Easy Create Read Update Delete
  • 9. ActiveRecord the ruby library I have never seen an Active Record implementation as complete or as useful as rails. - Martin Fowler
  • 10. Rails’ ActiveRecord • DRY Conventions & Assumptions • Validations • Before and after filters • Database Agnostic (mostly) • Migrations • Model relationships • has_many, belongs_to, etc...
  • 11. What Active Record Likes • mapping class names to table names • pluralized table names • integer primary keys • classname_id foreign keys • simple schemas • single table inheritance
  • 12. Active Record Doesn’t Like • views • stored methods • foreign key constraints • cascading commits • split or clustered db’s • enums
  • 13. The Basics ./app/models/user.rb Loading a user class User < ActiveRecord::Base >> user_obj = User.find(2) end => #<User:0x352e8bc @attributes= {"salt"=>"d9ef...", The SQL Log "updated_at"=>"2007-04-19 10:49:15", "crypted_password"=>"9c1...", User Load (0.003175) "id"=>"2", SELECT * FROM users "remember_token"=>"a8d...", WHERE (users.id = 2) LIMIT 1 "login"=>"rabble", "created_at"=>"2007-04-19 10:49:15", "email"=>"evan@protest.net"}>
  • 14. The Find Method Find is the primary method of Active Record Examples: User.find(23) User.find(:first) User.find(:all, :offset => 10, :limit => 10) User.find(:all, :include => [:account, :friends]) User.find(:all, :conditions => [“category in (?), categories, :limit => 50) User.find(:first).articles
  • 15. The Four Ways of Find Find by id: This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]). Find first: This will return the first record matched by the options used. Find all: This will return all the records matched by the options used. Indirectly: The find method is used for AR lookups via associations.
  • 16. Understanding Find Model#find(:all, { parameters hash } What Find Does: * generates sql * executes sql * returns an enumerable (array like object) * creates an AR model object for each row
  • 17. Find with :conditions :conditions - An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. Student.find(:all, :conditions => [‘first_name = ? and status = ?’ ‘rabble’, 1]) New Style (Edge Rails Only) Student.find(:all, :conditions => {:first_name => “rabble”, :status => 1}) SQL Executed: SELECT * FROM students WHERE (first_name = 'rabble' and status = 1);
  • 18. Doing it Securely class User < ActiveRecord::Base def self.authenticate_unsafely(user_name, password) find(:first, :conditions => "user_name = '#{user_name}' AND password = '#{password}'") end def self.authenticate_safely(user_name, password) find(:first, :conditions => [ "user_name = ? AND password = ?", user_name, password ]) end # Edge Rails Only (Next Version of Rails) def self.authenticate_safely_simply(user_name, password) find(:first, :conditions => { :user_name => user_name, :password => password }) end end
  • 19. Order By :order - An SQL fragment like "created_at DESC, name". Student.find(:all, :order => ‘updated_at DESC’) SQL Executed: SELECT * FROM users ORDER BY created_at;
  • 20. Group By :group - An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause. Student.find(:all, :group => ‘graduating_class’) SQL Executed: SELECT * FROM users GROUP BY graduating_class;
  • 21. Limit & Offset :limit - An integer determining the limit on the number of rows that should be returned. :offset- An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows. Student.find(:all, :limit => 10, :offset => 0) SQL Executed: SELECT * FROM users LIMIT 0, 10;
  • 22. Joins :joins - An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed). Student.find(:all, :join => "LEFT JOIN comments ON comments.post_id = id") SQL Executed: SELECT * FROM users GROUP BY graduating_class; Returns read only objects unless you say :readonly => false
  • 24. Associations The Four Primary Associations belongs_to has_one has_many has_and_belongs_to_many class Project < ActiveRecord::Base belongs_to :portfolio has_one :project_manager has_many :milestones has_and_belongs_to_many :categories end
  • 25. Associations One to One has_one & belongs_to Many to One has_many & belongs_to Many to Many has_and_belongs_to_many has_many :through
  • 26. One to One Use has_one in the base, and belongs_to in the associated model. class Employee < ActiveRecord::Base has_one :office end class Office < ActiveRecord::Base belongs_to :employee # foreign key - employee_id end
  • 27. One To One Example >> joe_employee = Employee.find_by_first_name('joe') SELECT * FROM employees WHERE (employees.`first_name` = 'joe') LIMIT 1 => #<Employee:0x36beb14 @attributes={"id"=>"1", "first_name"=>"joe", "last_name"=>"schmo", "created_at"=>"2007-04-21 09:08:59"}> >> joes_office = joe_employee.office SELECT * FROM offices WHERE (offices.employee_id = 1) LIMIT 1 => #<Office:0x36bc06c @attributes={"employee_id"=>"1", "id"=>"1", "created_at"=>"2007-04-21 09:11:44", "location"=>"A4302"}> >> joes_office.employee SELECT * FROM employees WHERE (employees.`id` = 1) => #<Employee:0x36b6ef0 @attributes={"id"=>"1", "first_name"=>"joe", "last_name"=>"schmo", "created_at"=>"2007-04-21 09:08:59"}>
  • 28. belongs_to One to One Relationship. Use belong to when the foreign key is in THIS table. • Post#author (similar to Author.find(author_id) ) • Post#author=(author) (similar to post.author_id = author.id) • Post#author? (similar to post.author == some_author) • Post#author.nil? • Post#build_author (similar to post.author = Author.new) • Post#create_author (similar to post.author = Author; post.author.save;
  • 29. Defining belongs_to class Employee < ActiveRecord::Base belongs_to :firm, :foreign_key => "client_of" belongs_to :author, :class_name => "Person", :foreign_key => "author_id" belongs_to :valid_coupon, :class_name => "Coupon", :foreign_key => "coupon_id", :conditions => 'discounts > #{payments_count}' belongs_to :attachable, :polymorphic => true end
  • 30. has_one One to One Relationship. Use has_one when the foreign key is in the OTHER table. • Account#beneficiary (similar to Beneficiary.find (:first, :conditions => "account_id = #{id}")) • Account#beneficiary=(beneficiary) (similar to beneficiary.account_id = account.id; beneficiary.save) • Account#beneficiary.nil? • Account#build_beneficiary (similar to Beneficiary.new ("account_id" => id)) • Account#create_beneficiary (similar to b = Beneficiary.new("account_id" => id); b.save; b)
  • 31. Defining has_one class Employee < ActiveRecord::Base # destroys the associated credit card has_one :credit_card, :dependent => :destroy # updates the associated records foreign key value to null rather than destroying it has_one :credit_card, :dependent => :nullify has_one :last_comment, :class_name => "Comment", :order => "posted_on" has_one :project_manager, :class_name => "Person", :conditions => "role = 'project_manager'" has_one :attachment, :as => :attachable end
  • 32. One to Many One-to-many Use has_many in the base, and belongs_to in the associated model. class Manager < ActiveRecord::Base has_many :employees end class Employee < ActiveRecord::Base belongs_to :manager # foreign key - manager_id end
  • 33. One to Many >> benevolent_dictator = Manager.find(:first, :conditions => ['name = "DHH"']) SELECT * FROM managers WHERE (name = "DHH") LIMIT 1 => #<Manager:0x369b7b8 @attributes={"name"=>"DHH", "id"=>"1", "created_at"=>"2007-04-21 09:59:24"}> >> minions = benevolent_dictator.employees SELECT * FROM employees WHERE (employees.manager_id = 1) => [#<Employee:0x36926a4 @attributes={"manager_id"=>"1", "id"=>"1", "first_name"=>"joe", "last_name"=>"schmo", "created_at"=>"2007-04-21 09:08:59"}>, #<Employee:0x36925f0 @attributes={"manager_id"=>"1", "id"=>"2", "first_name"=>"funky", "last_name"=>"monkey", "created_at"=>"2007-04-21 09:58:20"}>]
  • 34. has_many Augmenting the Model • Firm#clients (similar to Clients.find :all, :conditions => "firm_id = #{id}") • Firm#clients<< • Firm#clients.delete • Firm#client_ids • Firm#client_ids= • Firm#clients=
  • 35. has_many • Firm#client.clear • Firm#clients.empty? (similar to firm.clients.size == 0) • Firm#clients.size (similar to Client.count "firm_id = #{id}") • Firm#clients.find (similar to Client.find(id, :conditions => "firm_id = #{id}")) • Firm#clients.build (similar to Client.new ("firm_id" => id)) • Firm#clients.create (similar to c = Client.new ("firm_id" => id); c.save; c)
  • 36. has_many examples class Employee < ActiveRecord::Base has_many :comments, :order => "posted_on" has_many :comments, :include => :author has_many :people, :class_name => "Person", :conditions => "deleted = 0", :order => "name" has_many :tracks, :order => "position", :dependent => :destroy has_many :comments, :dependent => :nullify has_many :tags, :as => :taggable has_many :subscribers, :through => :subscriptions, :source => :user has_many :subscribers, :class_name => "Person", :finder_sql => 'SELECT DISTINCT people.* ' + 'FROM people p, post_subscriptions ps ' + 'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' + 'ORDER BY p.first_name' end
  • 37. Many to Many Simple Joiner Table has_and_belongs_to_many Joiner Model has_many :through
  • 38. has_and_belongs_to_many The Simple Joiner Table Way
  • 40. has_and_belongs_to_many Augmenting the Model • Developer#projects • Developer#projects<< • Developer#projects.delete • Developer#projects= • Developer#projects_ids • Developer#projects_ids= • Developer#clear
  • 41. has_and_belongs_to_many • Developer#projects.empty? • Developer#projects.size • Developer#projects.find(id) # Also find(:first / :all) • Developer#projects.build #(similar to Project.new ("project_id" => id)) • Developer#projects.create (similar to c = Project.new("project_id" => id); c.save; c)
  • 42. habtm example create_table :developers do |t| t.column :name, :string t.column :created_at, :datetime end create_table :projects do |t| t.column :name, :string t.column :created_at, :datetime end create_table(:developers_projects, :id => false) do |t| t.column :developer_id, :integer t.column :project_id, :integer end
  • 43. habtm example >> d = Developer.find(1) SELECT * FROM developers WHERE (developers.`id` = 1) => #<Developer:0x32bc7dc @attributes={"name"=>"rabble", "id"=>"1", "created_at"=>nil}> >> d.projects SELECT * FROM projects INNER JOIN developers_projects ON projects.id = developers_projects.project_id WHERE (developers_projects.developer_id = 1 ) => [#<Project:0x3257cc4 @attributes= {"name"=>"ragi", "project_id"=>"1", "id"=>"1", "developer_id"=>"1", "created_at"=>nil}>, #<Project:0x3257c10 @attributes= {"name"=>"acts_as_autenticated", "project_id"=>"3", "id"=>"3", "developer_id"=>"1", "created_at"=>nil}>]
  • 44. has_many :through DHH’s One True Way of Many to Many
  • 46. has_many :through class Appearance < ActiveRecord::Base belongs_to :dancer belongs_to :movie end class Dancer < ActiveRecord::Base has_many :appearances, :dependent => true has_many :movies, :through => :appearances end class Movie < ActiveRecord::Base has_many :appearances, :dependent => true has_many :dancers, :through => :appearances end
  • 47. Validations class User < ActiveRecord::Base validates_confirmation_of :login, :password validates_confirmation_of :email, :message => "should match confirmation" validates_format_of :email, :with => /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})/i, :on = :create end
  • 48. Validations • Keeping Data Clean • In object validation of fields, calculated validations • Instead of key constraints • The database is for storage, the model is for the business logic • Kinds of validations, custom validations, etc...
  • 49. But Wait? • Aren’t format, presence, relationship validations supposed to be the database’s job? • Traditionally, yes. • ActiveRecord does constraints in the model, not the database
  • 50. But Why? • Validations Constraints are Business Logic • Business logic should be in the model • It makes things easy • End users can get useful error messages • Makes the postback pattern work well
  • 51. Data Integrity? • It’s still possible to do constraints in the db • But it’s not as necessary • Validations are constraints which make sense in terms of functionality of the app • The rails ways is to just use validations • Most DBA’s insist on foreign_key constraints
  • 52. What AR Returns? • Enumerable Objects (kind of like arrays) • Preselects and instantiates objects • Nifty methods: to_yaml, to_xml, to_json
  • 53. Output Formats ruby - inspect to_yaml #Employee:0x36926a4 --- !ruby/object:Employee @attributes= attributes: {manager_id=1, manager_id: 1 id=1, id: 1 first_name=joe, first_name: joe last_name=schmo, last_name: schmo created_at=2007-04-21 09:08:59} created_at: 2007-04-21 09:08:59 to_xml to_json ?xml version=1.0 encoding=UTF-8? {attributes: employee {manager_id: 1, created-at id: 1, type=datetime2007-04-21T09:08:59-07:00/ created-at first_name: joe, first-namejoe/first-name last_name: schmo, id type=integer1/id created_at: 2007-04-21 09:08:59}} last-nameschmo/last-name manager-id type=integer1/manager-id /employee
  • 54. Before After Callbacks * (-) save class Subscription ActiveRecord::Base * (-) valid? before_create :record_signup * (1) before_validation private * (2) before_validation_on_create def record_signup * (-) validate self.signed_up_on = Date.today * (-) validate_on_create end * (3) after_validation end * (4) after_validation_on_create * (5) before_save class Firm ActiveRecord::Base # Destroys the associated clients and * (6) before_create #people when the firm is destroyed * (-) create before_destroy { * (7) after_create |record| Person.destroy_all firm_id = #{record.id} } * (8) after_save before_destroy { |record| Client.destroy_all client_of = #{record.id} } end
  • 56. Special Fields * created_at * #{table_name}_count * position * created_on * parent_id * updated_at * lft * updated_on * rgt * lock_version * quote * type * template * id
  • 59. Flickr Photos Used: http://flickr.com/photos/brraveheart/114402291/ http://flickr.com/photos/ryangreenberg/57722319/ http://flickr.com/photos/bright/253175260/ http://flickr.com/photos/benandliz/11065337/ http://flickr.com/photos/good_day/63617697/ http://flickr.com/photos/gaspi/12944421/ http://flickr.com/photos/rickharris/416150393/ http://flickr.com/photos/thomashawk/221827536/ http://flickr.com/photos/babasteve/3322247/ http://flickr.com/photos/brianboulos/7707518/ http://flickr.com/photos/olivander/28058685/ http://flickr.com/photos/ross/28330560/ http://flickr.com/photos/brraveheart/44052308/ http://flickr.com/photos/emdot/45249090/ http://flickr.com/photos/ednothing/142393509/ http://flickr.com/photos/farhang/428136695/ http://flickr.com/photos/alltheaces/87505524/ http://flickr.com/photos/belljar/67877047/ http://flickr.com/photos/alfr3do/7436142/ http://flickr.com/photos/pulpolux/34545782/ http://flickr.com/photos/gdominici/57975123/ http://flickr.com/photos/monkeyc/107979135/ http://flickr.com/photos/josefstuefer/72512671/ http://flickr.com/photos/pedrosimoes7/449314732/ http://flickr.com/photos/uqbar/105440294/ http://flickr.com/photos/dincordero/405452471/ http://flickr.com/photos/auntiep/17135231/ http://flickr.com/photos/andidfl/203883534/ http://flickr.com/photos/einsame_spitze/406992131/ http://flickr.com/photos/ivanomak/434387836/ http://flickr.com/photos/beija-flor/63758047/ http://flickr.com/photos/nrvica/23858419/ http://flickr.com/photos/amerune/174617912/ http://flickr.com/photos/thespeak/137012632/ http://flickr.com/photos/hungry_i/47938311/ http://flickr.com/photos/santos/13952912/ http://flickr.com/photos/supermietzi/179962496/ http://flickr.com/photos/traveller2020/206931940/ http://flickr.com/photos/ko_an/318906221/
  • 60. Questions? Introduction to Active Record Evan ‘Rabble’ Henshaw-Plath evan@protest.net - Yahoo! Brickhouse anarchogeek.com - testingrails.com