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

Active Record is still magical

5f22129933f5f1c12a489131a33a3993?s=47 Neeraj Singh
October 12, 2013

Active Record is still magical

5f22129933f5f1c12a489131a33a3993?s=128

Neeraj Singh

October 12, 2013
Tweet

Transcript

  1. Active Record is still Magical Neeraj Singh Saturday, October 12,

    13
  2. $ bundle exec rake test_sqlite3 3684 runs, 10491 assertions, 0

    failures, 0 errors, 29 skips Saturday, October 12, 13
  3. # make sure we get the gem, not stdlib gem

    'minitest' require 'minitest' # active_support/test_case.rb Saturday, October 12, 13
  4. # Returns all instance methods starting with "test_". # Based

    on test_order, the methods are either sorted, # randomized(default), or run in parallel. def self.runnable_methods methods = methods_matching(/^test_/) case self.test_order when :parallel max = methods.size ParallelEach.new methods.sort.sort_by { rand max } when :random then max = methods.size methods.sort.sort_by { rand max } when :alpha, :sorted then methods.sort else raise "Unknown test_order: #{self.test_order.inspect}" end end Saturday, October 12, 13
  5. # Defines the order to run tests # (:random by

    default). Override # this or use a convenience method to change it # for your tests. def self.test_order :random end Saturday, October 12, 13
  6. module Minitest # :nodoc: def self.__run reporter, options # :nodoc:

    # FIXME: MT5's runnables is not ordered. This is # needed because # we have have tests have cross-class order- dependent bugs. suites = Runnable.runnables.sort_by { |ts| ts.name.to_s } parallel, serial = suites.partition { |s| s.test_order == :parallel } ParallelEach.new(parallel).map { |suite| suite.run reporter, options } + serial.map { |suite| suite.run reporter, options } end end # active_support/test_case.rb Saturday, October 12, 13
  7. # FIXME: MT5's runnables is not ordered. # This is

    needed because # we have have tests have cross-class # order-dependent bugs. suites = Runnable.runnables.sort_by { |ts| ts.name.to_s } Saturday, October 12, 13
  8. module Minitest # :nodoc: def self.__run reporter, options # :nodoc:

    # FIXME: MT5's runnables is not ordered. This is # needed because # we have have tests have cross-class order- dependent bugs. suites = Runnable.runnables.sort_by { |ts| ts.name.to_s } suites.each { |s| puts s } parallel, serial = suites.partition { |s| s.test_order == :parallel } ParallelEach.new(parallel).map { |suite| suite.run reporter, options } + serial.map { |suite| suite.run reporter, options } end end Saturday, October 12, 13
  9. CalculationsTest CallbacksOnMultipleActionsTest CallbacksTest CascadedEagerLoadingTest CheckEscapedYamlFixturesTest CheckSetTableNameFixturesTest CopyMigrationsTest CopyTableTest CoreTest CounterCacheTest

    CustomConnectionFixturesTest CustomNameForFixtureOrModelTest DatabaseConnectedJsonEncodingTest DatabaseConnectedXmlSerializationTest DatabaseStatementsTest DateTimeTest DefaultScopingTest DefaultStringsTest Saturday, October 12, 13
  10. Finished in 373.451968s, 9.8647 runs/s, 28.0920 assertions/s. 3684 runs, 10491

    assertions, 0 failures, 0 errors, 29 skips Saturday, October 12, 13
  11. Runnable.runnables.sort_by { |t| t.name.to_s } Saturday, October 12, 13

  12. Runnable.runnables.sort_by { |t| t.name.to_s } Runnable.runnables.sort_by { |t| t.name.to_s.reverse }

    Saturday, October 12, 13
  13. 3684 runs, 10446 assertions, 8 failures, 5 errors, 29 skips

    sqlite3 Saturday, October 12, 13
  14. 3924 runs, 11064 assertions, 25 failures, 41 errors, 22 skips

    postgresql Saturday, October 12, 13
  15. 3684 runs, 10534 assertions, 8 failures, 4 errors, 23 skips

    mysql2 Saturday, October 12, 13
  16. Help Active Record Saturday, October 12, 13

  17. “Any sufficiently advanced technology is indistinguishable from magic.” ― Arthur

    C. Clarke, Saturday, October 12, 13
  18. irb(main):> User.where id: '45' SELECT "users".* FROM "users" WHERE "users"."id"

    = 45 Saturday, October 12, 13
  19. irb(main):> User.where id: '45' SELECT "users".* FROM "users" WHERE "users"."id"

    = 45 irb(main):> User.where name: 10 SELECT "users".* FROM "users" WHERE "users"."name" = 10 Saturday, October 12, 13
  20. Saturday, October 12, 13

  21. Saturday, October 12, 13

  22. class Physician < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments

    end Saturday, October 12, 13
  23. class Physician < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments

    end irb(main):> physician.appointments.create! INSERT INTO "appointments" ("physician_id") VALUES ($1) RETURNING "id" [["physician_id", 1]] => #<Appointment id: 3, physician_id: 1, patient_id: nil> Saturday, October 12, 13
  24. class Physician < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments

    end Saturday, October 12, 13
  25. class Physician < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments

    end irb(main):> physician.patients.create! INSERT INTO "patients" DEFAULT VALUES RETURNING "id" INSERT INTO "appointments" ("patient_id", "physician_id") VALUES ($1, $2) RETURNING "id" [["patient_id", 2], ["physician_id", 1]] => #<Patient id: 2, name: nil> Saturday, October 12, 13
  26. class Physician < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments

    end Saturday, October 12, 13
  27. class Physician < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments

    end irb(main):> physician.patients.where(id: 50).create! INSERT INTO "patients" ("id") VALUES ($1) RETURNING "id" [["id", 50]] => #<Patient id: 50, name: nil> Saturday, October 12, 13
  28. irb(main):> physician.patients.where(id: 50).class.name => "ActiveRecord::AssociationRelation" Saturday, October 12, 13

  29. irb(main):> physician.patients.class.name => "ActiveRecord::Associations::CollectionProxy" irb(main):> physician.patients.where(id: 50).class.name => "ActiveRecord::AssociationRelation" Saturday,

    October 12, 13
  30. irb(main):> Physician.all.class.name => "ActiveRecord::Relation" Saturday, October 12, 13

  31. irb(main):> Physician.all .where(id: 2) .where(name: 'John') .class.name => "ActiveRecord::Relation" irb(main):>

    Physician.all.class.name => "ActiveRecord::Relation" Saturday, October 12, 13
  32. class CollectionProxy < Relation end Saturday, October 12, 13

  33. A collectionProxy has reference to the association it is acting

    on class CollectionProxy < Relation end Saturday, October 12, 13
  34. A collectionProxy has reference to the association it is acting

    on class CollectionProxy < Relation end irb(main):> Physician.last .patients .instance_variable_get(:@association) .class => ActiveRecord::Associations::HasManyThroughAssociation Saturday, October 12, 13
  35. irb(main):> physician.patients.where(id: 50).create! INSERT INTO "patients" ("id") VALUES ($1) RETURNING

    "id" [["id", 50]] => #<Patient id: 50, name: nil> Saturday, October 12, 13
  36. def create(*args, &block) scoping { @klass.create(*args, &block) } end irb(main):>

    physician.patients.where(id: 50).create! INSERT INTO "patients" ("id") VALUES ($1) RETURNING "id" [["id", 50]] => #<Patient id: 50, name: nil> Saturday, October 12, 13
  37. def create!(attributes = {}, &block) @association.create!(attributes, &block) end def create(*args,

    &block) scoping { @klass.create(*args, &block) } end irb(main):> physician.patients.where(id: 50).create! INSERT INTO "patients" ("id") VALUES ($1) RETURNING "id" [["id", 50]] => #<Patient id: 50, name: nil> Saturday, October 12, 13
  38. Saturday, October 12, 13

  39. class Car < ActiveRecord::Base scope :good, where(:id => 1) scope

    :bad, where(:id => 2) end Saturday, October 12, 13
  40. class Car < ActiveRecord::Base scope :good, where(:id => 1) scope

    :bad, where(:id => 2) end # using scopes Car.good.bad SELECT "cars".* FROM "cars" WHERE "cars"."id" = 2 Saturday, October 12, 13
  41. class Car < ActiveRecord::Base scope :good, where(:id => 1) scope

    :bad, where(:id => 2) end # using scopes Car.good.bad SELECT "cars".* FROM "cars" WHERE "cars"."id" = 2 # using scopes Car.bad.good SELECT "cars".* FROM "cars" WHERE "cars"."id" = 1 Saturday, October 12, 13
  42. class Car < ActiveRecord::Base scope :good, where(:id => 1) scope

    :bad, where(:id => 2) end # using scopes Car.good.bad SELECT "cars".* FROM "cars" WHERE "cars"."id" = 2 # using wheres Car.where(:id => 1).where(:id => 2) SELECT "cars".* FROM "cars" WHERE "cars"."id" = 1 AND "cars"."id" = 2 # using scopes Car.bad.good SELECT "cars".* FROM "cars" WHERE "cars"."id" = 1 Saturday, October 12, 13
  43. Saturday, October 12, 13

  44. Saturday, October 12, 13

  45. class User < ActiveRecord::Base end Saturday, October 12, 13

  46. class User < ActiveRecord::Base end irb(main):> User.where(name: 'john') .order('id desc')

    .except(:order) => SELECT "users".* FROM "users" WHERE "users"."name" = 'john' Saturday, October 12, 13
  47. class User < ActiveRecord::Base default_scope { where(active: true) } end

    Saturday, October 12, 13
  48. class User < ActiveRecord::Base default_scope { where(active: true) } end

    irb(main):> User.where(name: 'john') .order('id desc') .except(:order) => SELECT "users".* FROM "users" WHERE "users"."active" = 't' AND "users"."name" = 'john' Saturday, October 12, 13
  49. class User < ActiveRecord::Base default_scope { where(active: true) } end

    irb(main):> User.where(name: 'john') .order('id desc') .except(:order) => SELECT "users".* FROM "users" WHERE "users"."active" = 't' AND "users"."name" = 'john' irb(main):> User.order('id desc') .except(:where) => SELECT "users".* FROM "users" ORDER BY id desc Saturday, October 12, 13
  50. class User < ActiveRecord::Base default_scope { where(active: true) } scope

    :signed_in_2013, -> { except(:where) .where(created_at: Time.now.all_year) } end Saturday, October 12, 13
  51. class User < ActiveRecord::Base default_scope { where(active: true) } scope

    :signed_in_2013, -> { except(:where) .where(created_at: Time.now.all_year) } end irb(main):> User.signed_in_2013 => SELECT "users".* FROM "users" WHERE "users"."active" = 't' AND ("users"."created_at" BETWEEN '2013-01-01 05:00:00.000000' AND '2014-01-01 04:59:59.999999') Saturday, October 12, 13
  52. class User < ActiveRecord::Base default_scope { where(active: true) } scope

    :signed_in_2013, -> { except(:where) .where(created_at: Time.now.all_year) } end irb(main):> User.signed_in_2013 => SELECT "users".* FROM "users" WHERE "users"."active" = 't' AND ("users"."created_at" BETWEEN '2013-01-01 05:00:00.000000' AND '2014-01-01 04:59:59.999999') In Rails 4.0.0 default_scope is applied lazily at the very end of the relation. rails 4.0.0 Saturday, October 12, 13
  53. class User < ActiveRecord::Base default_scope { where(active: true) } scope

    :signed_in_2013, -> { except(:where) .where(created_at: Time.now.all_year) } end Saturday, October 12, 13
  54. class User < ActiveRecord::Base default_scope { where(active: true) } scope

    :signed_in_2013, -> { except(:where) .where(created_at: Time.now.all_year) } end irb(main):> User.signed_in_2013 => SELECT "users".* FROM "users" AND ("users"."created_at" BETWEEN '2013-01-01 05:00:00.000000' AND '2014-01-01 04:59:59.999999') rails edge Saturday, October 12, 13
  55. class User < ActiveRecord::Base default_scope { where(active: true) } scope

    :signed_in_2013, -> { unscoped.where(created_at: Time.now.all_year) } end Saturday, October 12, 13
  56. class User < ActiveRecord::Base default_scope { where(active: true) } scope

    :signed_in_2013, -> { unscoped.where(created_at: Time.now.all_year) } end class User < ActiveRecord::Base default_scope { where(active: true) } scope :signed_in_2013, -> { build_default_scope.except(:where). where(created_at: Time.now.all_year) } end Saturday, October 12, 13
  57. Saturday, October 12, 13

  58. class User < ActiveRecord::Base has_many :posts end Saturday, October 12,

    13
  59. class User < ActiveRecord::Base has_many :posts end irb(main):> User.preload(:posts).to_a User

    Load (0.7ms) SELECT "users".* FROM "users" SELECT "posts".* FROM "posts" WHERE "posts"."user_id" IN (1) Saturday, October 12, 13
  60. class User < ActiveRecord::Base has_many :posts end irb(main):> User.preload(:posts).to_a User

    Load (0.7ms) SELECT "users".* FROM "users" SELECT "posts".* FROM "posts" WHERE "posts"."user_id" IN (1) - preload loads the association data in separate queries - this is how include works in default case Saturday, October 12, 13
  61. irb(main):> User.preload(:posts) .where("posts.desc='ruby is awesome'") => PG::Error: ERROR: missing FROM-clause

    entry for table "posts" Saturday, October 12, 13
  62. irb(main):> User.preload(:posts) .where("posts.desc='ruby is awesome'") => PG::Error: ERROR: missing FROM-clause

    entry for table "posts" irb(main):> User.preload(:posts) .where("users.name='John'") => SELECT "users".* FROM "users" WHERE (users.name='John') Saturday, October 12, 13
  63. irb(main):> User.eager_load(:posts) SELECT "users"."id" AS t0_r0, "users"."name" AS t0_r1, "users"."email"

    AS t0_r2, "users"."active" AS t0_r3, "users"."created_at" AS t0_r4, "users"."updated_at" AS t0_r5, "posts"."id" AS t1_r0, "posts"."name" AS t1_r1, "posts"."title" AS t1_r2, "posts"."user_id" AS t1_r3 FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id" Saturday, October 12, 13
  64. irb(main):> User.includes :posts SELECT "users".* FROM "users" SELECT "posts".* FROM

    "posts" WHERE "posts"."user_id" IN (1) Saturday, October 12, 13
  65. irb(main):> User.includes(:posts).references(:posts) SELECT "users"."id" AS t0_r0, "users"."name" AS t0_r1, "users"."email"

    AS t0_r2, "users"."active" AS t0_r3, "users"."created_at" AS t0_r4, "users"."updated_at" AS t0_r5, "posts"."id" AS t1_r0, "posts"."name" AS t1_r1, "posts"."title" AS t1_r2, "posts"."user_id" AS t1_r3 FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id" Saturday, October 12, 13
  66. irb(main):> User.includes(:posts) .where('posts.name' => "ruby is awesome") SELECT "users"."id" AS

    t0_r0, "users"."name" AS t0_r1, "users"."email" AS t0_r2, "users"."active" AS t0_r3, "users"."created_at" AS t0_r4, "users"."updated_at" AS t0_r5, "posts"."id" AS t1_r0, "posts"."name" AS t1_r1, "posts"."title" AS t1_r2, "posts"."user_id" AS t1_r3 FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id" WHERE "posts"."name" = 'ruby is awesome' Saturday, October 12, 13
  67. irb(main):> User.joins(:posts) SELECT "users".* FROM "users" INNER JOIN "posts" ON

    "posts"."user_id" = "users"."id" Saturday, October 12, 13
  68. Saturday, October 12, 13

  69. class User < ActiveRecord::Base has_many :posts end Saturday, October 12,

    13
  70. class User < ActiveRecord::Base has_many :posts end irb(main):> user =

    User.create! Saturday, October 12, 13
  71. class User < ActiveRecord::Base has_many :posts end irb(main):> user =

    User.create! irb(main):> user.posts.create! name: 'ruby' Saturday, October 12, 13
  72. class User < ActiveRecord::Base has_many :posts end irb(main):> user =

    User.create! irb(main):> user.posts.create! name: 'ruby' irb(main):> user.posts[0].name = 'rails' Saturday, October 12, 13
  73. class User < ActiveRecord::Base has_many :posts end irb(main):> user =

    User.create! irb(main):> user.posts.create! name: 'ruby' irb(main):> user.posts[0].name = 'rails' irb(main):> user.save DEBUG -- : (0.4ms) BEGIN DEBUG -- : (0.3ms) COMMIT => true Saturday, October 12, 13
  74. class User < ActiveRecord::Base has_many :posts, autosave: true end Saturday,

    October 12, 13
  75. class User < ActiveRecord::Base has_many :posts, autosave: true end irb(main):>

    user = User.create! Saturday, October 12, 13
  76. class User < ActiveRecord::Base has_many :posts, autosave: true end irb(main):>

    user = User.create! irb(main):> user.posts.create! name: 'ruby' Saturday, October 12, 13
  77. class User < ActiveRecord::Base has_many :posts, autosave: true end irb(main):>

    user.posts[0].name = 'rails' irb(main):> user = User.create! irb(main):> user.posts.create! name: 'ruby' Saturday, October 12, 13
  78. class User < ActiveRecord::Base has_many :posts, autosave: true end irb(main):>

    user.posts[0].name = 'rails' irb(main):> user.save UPDATE "posts" SET "name" = $1 WHERE "posts"."id" = 5 [["name", "rails"]] irb(main):> user = User.create! irb(main):> user.posts.create! name: 'ruby' Saturday, October 12, 13
  79. class Post < ActiveRecord::Base belongs_to :user, autosave: true end Saturday,

    October 12, 13
  80. irb(main):> post.user.name = 'John' class Post < ActiveRecord::Base belongs_to :user,

    autosave: true end Saturday, October 12, 13
  81. irb(main):> post.user.name = 'John' class Post < ActiveRecord::Base belongs_to :user,

    autosave: true end irb(main):> post.save UPDATE "users" SET "name" = $1, "updated_at" = $2 WHERE "users"."id" = 4 [["name", "John"], ["updated_at", Thu, 10 Oct 2013 03:35:01 UTC +00:00]] Saturday, October 12, 13
  82. class Post < ActiveRecord::Base belongs_to :user, autosave: true before_save :add_salutation

    def add_salutation self.user.name = 'Hello ' + self.user.name end end Saturday, October 12, 13
  83. class Post < ActiveRecord::Base belongs_to :user, autosave: true before_save :add_salutation

    def add_salutation self.user.name = 'Hello ' + self.user.name end end class Post < ActiveRecord::Base # before_belongs_to definition before_save :add_salutation belongs_to :user, autosave: true def add_salutation self.user.name = 'Hello ' + self.user.name end end Saturday, October 12, 13
  84. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user validates :name, presence: true end Saturday, October 12, 13
  85. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user validates :name, presence: true end Saturday, October 12, 13
  86. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user validates :name, presence: true end irb(main):> post = user.posts.create name: 'rails' Saturday, October 12, 13
  87. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user validates :name, presence: true end irb(main):> post = user.posts.create name: 'rails' irb(main):> post.name = nil => nil Saturday, October 12, 13
  88. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user validates :name, presence: true end irb(main):> post = user.posts.create name: 'rails' irb(main):> post.name = nil => nil irb(main):028:0> post.valid? => false Saturday, October 12, 13
  89. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user validates :name, presence: true end irb(main):> post = user.posts.create name: 'rails' irb(main):> post.name = nil => nil irb(main):028:0> post.valid? => false irb(main):029:0> user.valid? => true Saturday, October 12, 13
  90. class User < ActiveRecord::Base has_many :posts, autosave: true end class

    Post < ActiveRecord::Base belongs_to :user validates :name, presence: true end Saturday, October 12, 13
  91. class User < ActiveRecord::Base has_many :posts, autosave: true end class

    Post < ActiveRecord::Base belongs_to :user validates :name, presence: true end irb(main):> post = user.posts.create name: 'rails' Saturday, October 12, 13
  92. class User < ActiveRecord::Base has_many :posts, autosave: true end class

    Post < ActiveRecord::Base belongs_to :user validates :name, presence: true end irb(main):> post = user.posts.create name: 'rails' irb(main):> post.name = nil => nil Saturday, October 12, 13
  93. class User < ActiveRecord::Base has_many :posts, autosave: true end class

    Post < ActiveRecord::Base belongs_to :user validates :name, presence: true end irb(main):> post = user.posts.create name: 'rails' irb(main):> post.name = nil => nil irb(main):028:0> post.valid? => false Saturday, October 12, 13
  94. class User < ActiveRecord::Base has_many :posts, autosave: true end class

    Post < ActiveRecord::Base belongs_to :user validates :name, presence: true end irb(main):> post = user.posts.create name: 'rails' irb(main):> post.name = nil => nil irb(main):028:0> post.valid? => false irb(main):029:0> user.valid? => false Saturday, October 12, 13
  95. Saturday, October 12, 13

  96. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user end Saturday, October 12, 13
  97. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user end irb(main):> user.object_id => 70196526096800 irb(main):> user.posts.first.user.object_id SELECT "posts".* FROM "posts" WHERE .......... SELECT "users".* FROM "users" WHERE .......... => 70196526747120 Saturday, October 12, 13
  98. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user end irb(main):> user.object_id => 70196526096800 irb(main):> user.posts.first.user.object_id SELECT "posts".* FROM "posts" WHERE .......... SELECT "users".* FROM "users" WHERE .......... => 70196526747120 rails 4.0.0 Saturday, October 12, 13
  99. irb(main):> user.object_id => 70196526096800 irb(main):> user.posts.first.user.object_id SELECT "posts".* FROM "posts"

    WHERE .......... SELECT "users".* FROM "users" WHERE .......... => 70196526747120 Saturday, October 12, 13
  100. irb(main):> user.object_id => 70196526096800 irb(main):> user.posts.first.user.object_id SELECT "posts".* FROM "posts"

    WHERE .......... SELECT "users".* FROM "users" WHERE .......... => 70196526747120 rails 4.0.0 Saturday, October 12, 13
  101. irb(main):> user.object_id => 70196526096800 irb(main):> user.posts.first.user.object_id SELECT "posts".* FROM "posts"

    WHERE .......... SELECT "users".* FROM "users" WHERE .......... => 70196526747120 rails 4.0.0 irb(main):> user.object_id => 70196526096800 irb(main):> user.posts.first.user.object_id SELECT "posts".* FROM "posts" WHERE .......... => 70196526096800 rails edge Saturday, October 12, 13
  102. inverse_of is auto derived in rails 4.1 Saturday, October 12,

    13
  103. inverse_of is auto derived in rails 4.1 rails edge Saturday,

    October 12, 13
  104. If you do not set the :inverse_of record, the association

    will do its best to match itself up with the correct inverse. Saturday, October 12, 13
  105. If you do not set the :inverse_of record, the association

    will do its best to match itself up with the correct inverse. rails edge Saturday, October 12, 13
  106. However, associations that contain the following options will not have

    their inverses set automatically :conditions :through :polymorphic :foreign_key Saturday, October 12, 13
  107. However, associations that contain the following options will not have

    their inverses set automatically :conditions :through :polymorphic :foreign_key rails edge Saturday, October 12, 13
  108. Saturday, October 12, 13

  109. class User < ActiveRecord::Base end User.new.save Saturday, October 12, 13

  110. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  111. # module Persistence def save(*) create_or_update rescue ActiveRecord::RecordInvalid false end

    Saturday, October 12, 13
  112. # module Persistence def save(*) create_or_update rescue ActiveRecord::RecordInvalid false end

    def create_or_update raise ReadOnlyRecord if readonly? result = new_record? ? create_record : update_record result != false end Saturday, October 12, 13
  113. # module Validations def save(options={}) perform_validations(options) ? super : false

    end Saturday, October 12, 13
  114. # module AttributeMethods def save(*) if status = super @previously_changed

    = changes @changed_attributes.clear end status end Saturday, October 12, 13
  115. # module Transactions def save(*) #:nodoc: rollback_active_record_state! do with_transaction_returning_status {

    super } end end def rollback_active_record_state! remember_transaction_record_state yield rescue Exception restore_transaction_record_state raise ensure clear_transaction_record_state end Saturday, October 12, 13
  116. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  117. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  118. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  119. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  120. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  121. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  122. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  123. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  124. entering save in transactions entering save in attribute_methods entering save

    in validations entering save in persistence leaving save in persistence leaving save in validations leaving save in attribute_methods leaving save in transactions Life cycle of save in Active Record Saturday, October 12, 13
  125. Saturday, October 12, 13

  126. class Comment < ActiveRecord::Base belongs_to :post end class Author <

    ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :author has_many :comments end Comment.joins(:post) .merge(Post.joins(:author) .merge(Author.where(:name => "Joe"))).all Saturday, October 12, 13
  127. class Comment < ActiveRecord::Base belongs_to :post end class Author <

    ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :author has_many :comments end Comment.joins(:post) .merge(Post.joins(:author) .merge(Author.where(:name => "Joe"))).all Association named 'author' was not found on Comment Saturday, October 12, 13
  128. class Comment < ActiveRecord::Base belongs_to :post end class Author <

    ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :author has_many :comments end Comment.joins(:post) .merge(Post.joins(:author) .merge(Author.where(:name => "Joe"))).all Association named 'author' was not found on Comment rails 3.2.x Saturday, October 12, 13
  129. irb(main):> Post.joins(:author).merge( Author.where(:name => "J")).joins_values => [:author] irb(main):> Post.joins(:author).merge( Author.where(:name

    => "J")).where_values => [#<Arel::Nodes::Equality:0x007fe0b89598e0 @left=#<struct Arel::Attributes::Attribute relation=#<Arel::Table:0x007fe0bd55e4c0 @name="authors", @engine=Author(id: integer, name: string), @columns=nil, @aliases=[], @table_alias=nil, @primary_key=nil>, name="name">, @right="Joe">] Saturday, October 12, 13
  130. j = Comment.joins(:post).merge( Post.joins(:author).merge( Author.where(:name => "Joe"))).joins_values Saturday, October 12,

    13
  131. irb(main):> j.first => :post irb(main):> j.second => [] irb(main):> j.third.class.name

    "ActiveRecord::Associations::JoinDependency::JoinAsso ciation" irb(main):> j.third.reflection.class.name => "ActiveRecord::Reflection::AssociationReflection" irb(main):> j.third.tables => [#<Arel::Table:0x007fe0ba978f68 @name="authors", @engine=ActiveRecord::Base, @columns=nil, @aliases=[], @table_alias=nil, @primary_key=nil>] Saturday, October 12, 13
  132. irb(main):> j.first => :post rails 4.0.0 irb(main):> j.second => []

    irb(main):> j.third.class.name "ActiveRecord::Associations::JoinDependency::JoinAsso ciation" irb(main):> j.third.reflection.class.name => "ActiveRecord::Reflection::AssociationReflection" irb(main):> j.third.tables => [#<Arel::Table:0x007fe0ba978f68 @name="authors", @engine=ActiveRecord::Base, @columns=nil, @aliases=[], @table_alias=nil, @primary_key=nil>] Saturday, October 12, 13
  133. class Comment < ActiveRecord::Base belongs_to :post end class Author <

    ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :author has_many :comments end Comment.joins(:post) .merge(Post.joins(:author) .merge(Author.where(:name => "Joe"))).all Saturday, October 12, 13
  134. Neeraj Singh @neerajdotname @BigBinary Saturday, October 12, 13