Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

How should ActiveRecord behave with merging conditions from various sources? See https://github.com/rails/rails/issues/9813 for background.

  • clobber: conflicting condition values are replaced with the rightmost value
  • append: conflicting condition values are chained into an AND query

Direct find

class Person < ActiveRecord::Base
end
Example 3.2.12 3.2.14.rc2 Expected
Person.where(id: 1).find(2)
append append
Person.where(id: [1, 3]).find(2)
append append
Person.where("id = 1").find(2)
append append
Person.where(name: "a").find_by_name("b")
append append

Chained scopes

class Person < ActiveRecord::Base
  scope :sad, where(status: "sad")
  scope :confused, where(status: "confused")
  def self.frustrated
    where(status: "frustrated")
  end
end
Example 3.2.12 3.2.14.rc2 Expected
Person.where(status: "sad").where(status: "happy")
append append
Person.confused.where(status: "happy")
append append
Person.where(status: "happy").confused
clobber clobber
Person.sad.confused
clobber clobber
Person.sad.frustrated
append append

Associations

class Manufacturer < ActiveRecord::Base
  has_many :products
end

class Product < ActiveRecord::Base
  belongs_to :manufacturer
end
Example 3.2.12 3.2.14.rc2 Expected
Manufacturer.find(1).products.where(manufacturer_id: 2)
append append

Default Scopes

class Person < ActiveRecord::Base
  default_scope where(status: "meh")
  scope :confused, where(status: "confused")
  def self.frustrated
    where(status: "frustrated")
  end
end
Example 3.2.12 3.2.14.rc2 Expected
Person.confused
clobber clobber
Person.frustrated
clobber clobber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment