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

Active Record is still magical

Neeraj Singh
October 12, 2013

Active Record is still magical

Neeraj Singh

October 12, 2013
Tweet

More Decks by Neeraj Singh

Other Decks in Programming

Transcript

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

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

    'minitest' require 'minitest' # active_support/test_case.rb Saturday, October 12, 13
  3. # 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
  4. # 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
  5. 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
  6. # 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
  7. 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
  8. CalculationsTest CallbacksOnMultipleActionsTest CallbacksTest CascadedEagerLoadingTest CheckEscapedYamlFixturesTest CheckSetTableNameFixturesTest CopyMigrationsTest CopyTableTest CoreTest CounterCacheTest

    CustomConnectionFixturesTest CustomNameForFixtureOrModelTest DatabaseConnectedJsonEncodingTest DatabaseConnectedXmlSerializationTest DatabaseStatementsTest DateTimeTest DefaultScopingTest DefaultStringsTest Saturday, October 12, 13
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. A collectionProxy has reference to the association it is acting

    on class CollectionProxy < Relation end Saturday, October 12, 13
  16. 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
  17. 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
  18. 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
  19. 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
  20. class Car < ActiveRecord::Base scope :good, where(:id => 1) scope

    :bad, where(:id => 2) end Saturday, October 12, 13
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. irb(main):> User.includes :posts SELECT "users".* FROM "users" SELECT "posts".* FROM

    "posts" WHERE "posts"."user_id" IN (1) Saturday, October 12, 13
  39. 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
  40. 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
  41. irb(main):> User.joins(:posts) SELECT "users".* FROM "users" INNER JOIN "posts" ON

    "posts"."user_id" = "users"."id" Saturday, October 12, 13
  42. class User < ActiveRecord::Base has_many :posts end irb(main):> user =

    User.create! irb(main):> user.posts.create! name: 'ruby' Saturday, October 12, 13
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. class User < ActiveRecord::Base has_many :posts end class Post <

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

    ActiveRecord::Base belongs_to :user validates :name, presence: true end Saturday, October 12, 13
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. 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
  62. class User < ActiveRecord::Base has_many :posts end class Post <

    ActiveRecord::Base belongs_to :user end Saturday, October 12, 13
  63. 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
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. However, associations that contain the following options will not have

    their inverses set automatically :conditions :through :polymorphic :foreign_key Saturday, October 12, 13
  71. 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
  72. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  73. # 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
  74. # module AttributeMethods def save(*) if status = super @previously_changed

    = changes @changed_attributes.clear end status end Saturday, October 12, 13
  75. # 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
  76. module ActiveRecord class Base ...................... include Persistence ....................... include Validations

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

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

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

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

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

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

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

    ........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
  84. 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
  85. 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
  86. 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
  87. 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
  88. 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
  89. 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
  90. 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
  91. 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