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
# 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
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
# 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
on class CollectionProxy < Relation end irb(main):> Physician.last .patients .instance_variable_get(:@association) .class => ActiveRecord::Associations::HasManyThroughAssociation Saturday, October 12, 13
: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
: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
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
: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
: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
: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
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
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
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
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
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
Post < ActiveRecord::Base belongs_to :user validates :name, presence: true end irb(main):> post = user.posts.create name: 'rails' Saturday, October 12, 13
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
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
........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
def create_or_update raise ReadOnlyRecord if readonly? result = new_record? ? create_record : update_record result != false end Saturday, October 12, 13
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
........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
........................ include AttributeMethods ........................ include Transactions ........................ end end Saturday, October 12, 13
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
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
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
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
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