Skip to content
Commits on Jan 22, 2016
  1. @tenderlove

    Eliminate instance level writers for class accessors

    tenderlove committed
    Instance level writers can have an impact on how the Active Model /
    Record objects are saved.  Specifically, they can be used to bypass
    validations.  This is a problem if mass assignment protection is
    disabled and specific attributes are passed to the constructor.
Commits on Sep 7, 2015
  1. @dmitry

    Validate multiple contexts on `valid?` and `invalid?` at once.

    dmitry committed
    class Person
      include ActiveModel::Validations
      attr_reader :name, :title
      validates_presence_of :name, on: :create
      validates_presence_of :title, on: :update
    person =
    person.valid?([:create, :update])    # => true
    person.errors.messages               # => {:name=>["can't be blank"], :title=>["can't be blank"]}
  2. @rafaelfranca

    Revert "Merge pull request #21069 from dmitry/feature/validate-multip…

    rafaelfranca committed
    This reverts commit 51dd258, reversing
    changes made to ecb4e4b.
    This broke Active Record tests
Commits on Jul 30, 2015
  1. @dmitry

    Validate multiple contexts on `valid?` and `invalid?` at once.

    dmitry committed
    class Person
      include ActiveModel::Validations
      attr_reader :name, :title
      validates_presence_of :name, on: :create
      validates_presence_of :title, on: :update
    person =
    person.valid?([:create, :update])    # => true
    person.errors.messages               # => {:name=>["can't be blank"], :title=>["can't be blank"]}
Commits on Jul 15, 2015
  1. @tgxworld

    Revert "Revert "Reduce allocations when running AR callbacks.""

    tgxworld committed
    This reverts commit bdc1d32.
    Calculating -------------------------------------
                            22.000  i/100ms
                            229.700  (± 0.4%) i/s -      1.166k
    Total Allocated Object: 9939
    Calculating -------------------------------------
                            24.000  i/100ms
                            246.443  (± 0.8%) i/s -      1.248k
    Total Allocated Object: 7939
      require 'bundler/inline'
    rescue LoadError => e
      $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
      raise e
    gemfile(true) do
      source '';
      # gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3'
      gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7'
      gem 'arel', github: 'rails/arel'
      gem 'sqlite3'
      gem 'benchmark-ips'
    require 'active_record'
    require 'benchmark/ips'
    ActiveRecord::Migration.verbose = false
    ActiveRecord::Schema.define do
      create_table :users, force: true do |t|
        t.string :name, :email
        t.boolean :admin
        t.timestamps null: false
    class User < ActiveRecord::Base
      default_scope { where(admin: true) }
    admin = true
    1000.times do
      attributes = {
        name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        email: "",
        admin: admin
      admin = !admin
    Benchmark.ips(5, 3) do |x| { User.all.to_a }
    key =
      if RUBY_VERSION < '2.2'
    before = GC.stat[key]
    after = GC.stat[key]
    puts "Total Allocated Object: #{after - before}"
Commits on Jul 7, 2015
  1. @senny

    docs, clarify the meanaing of return values from validation methods.

    senny committed
    [ci skip]
    Closes #20792.
    Custom validation methods are implemented in terms of
    callbacks. The `validate` callback chain can't be halted using return
    values of individual callbacks.
  2. @senny

    docs, remove accidental :nodoc: of ActiveModel::Validations::ClassMet…

    senny committed
    …hods methods.
    [ci skip]
    While this :nodoc: did hide the constant it also removed the following
    methods from the API docs:
    - #attribute_method?
    - #clear_validators!
    - #validate
    - #validators
    - #validators_on
    Those are public API and should be visible.
    Issue was caused by dee4fbc
    /cc @zzak
Commits on Apr 26, 2015
  1. @zzak
Commits on Mar 22, 2015
  1. @tgxworld

    Revert "Reduce allocations when running AR callbacks."

    tgxworld committed
    This reverts commit 796cab4.
Commits on Feb 20, 2015
  1. @lucasmazza
Commits on Dec 12, 2014
  1. @chancancode

    Pass through the `prepend` option to `AS::Callback`

    chancancode committed
    I'm not sure what's the use case for this, but apparently it broke some apps.
    Since it was not the intended result from #16210 I fixed it to not raise an
    exception anymore. However, I didn't add documentation for it because I don't
    know if this should be officially supported without knowing how it's meant to
    be used.
    In general, validations should be side-effect-free (other than adding to the
    error message to `@errors`). Order-dependent validations seems like a bad idea.
    Fixes #18002
Commits on Oct 26, 2014
  1. @rafaelfranca

    Prefix internal method with _

    rafaelfranca committed
    This will avoid naming clash with user defined methods
Commits on Oct 4, 2014
  1. @zzak

    Merge pull request #16409 from justinweiss/update_validation_context_…

    zzak committed
    Docs: Add a note on custom validation contexts. [ci skip]
Commits on Sep 30, 2014
  1. @pabloh
Commits on Sep 28, 2014
  1. @phiggins

    Reduce allocations when running AR callbacks.

    phiggins committed
    Inspired by @tenderlove's work in
    c363fff, this reduces the number of
    strings allocated when running callbacks for ActiveRecord instances. I
    measured that using this script:
    require 'objspace'
    require 'active_record'
    require 'allocation_tracer'
    ActiveRecord::Base.establish_connection adapter: "sqlite3",
                                            database: ":memory:"
    ActiveRecord::Base.connection.instance_eval do
      create_table(:articles) { |t| t.string :name }
    class Article < ActiveRecord::Base; end
    a = Article.create name: "foo"
    a = Article.find
    N = 10
    result = ObjectSpace::AllocationTracer.trace do
      N.times { Article.find }
    result.sort.each do |k,v|
      p k => v
    puts "total: #{}"
    When I run this against master and this branch I get this output:
    pete@balloon:~/projects/rails/activerecord$ git checkout master
    M Gemfile
    Switched to branch 'master'
    pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_before
    pete@balloon:~/projects/rails/activerecord$ git checkout remove-dynamic-send-on-built-in-callbacks
    M Gemfile
    Switched to branch 'remove-dynamic-send-on-built-in-callbacks'
    pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_after
    pete@balloon:~/projects/rails/activerecord$ diff allocations_before allocations_after
    81]=>[40, 0, 0, 0, 0, 0]}
    < total: 630
    > total: 590
    In addition to this, there are two micro-optimizations present:
    * Using ` if block` vs `yield if block_given?` when the block was being captured already.
    pete@balloon:~/projects$ cat benchmark_block_call_vs_yield.rb
    require 'benchmark/ips'
    def block_capture_with_yield &block
      yield if block_given?
    def block_capture_with_call &block if block
    def no_block_capture
      yield if block_given?
    Benchmark.ips do |b|"block_capture_with_yield") { block_capture_with_yield }"block_capture_with_call") { block_capture_with_call }"no_block_capture") { no_block_capture }
    pete@balloon:~/projects$ ruby benchmark_block_call_vs_yield.rb
    Calculating -------------------------------------
                            124979 i/100ms
                            138340 i/100ms
        no_block_capture    136827 i/100ms
                          5703108.9 (±2.4%) i/s -   28495212 in   4.999368s
                          6840730.5 (±3.6%) i/s -   34169980 in   5.002649s
        no_block_capture  5821141.4 (±2.8%) i/s -   29144151 in   5.010580s
    * Defining and calling methods instead of using send.
    pete@balloon:~/projects$ cat benchmark_method_call_vs_send.rb
    require 'benchmark/ips'
    class Foo
      def tacos
    my_foo =
    Benchmark.ips do |b|'send') { my_foo.send('tacos') }'call') { my_foo.tacos }
    pete@balloon:~/projects$ ruby benchmark_method_call_vs_send.rb
    Calculating -------------------------------------
                    send     97736 i/100ms
                    call    151142 i/100ms
                    send  2683730.3 (±2.8%) i/s -   13487568 in   5.029763s
                    call  8005963.9 (±2.7%) i/s -   40052630 in   5.006604s
    The result of this is making typical ActiveRecord operations slightly faster:
Commits on Sep 22, 2014
  1. @chancancode

    Move the array to a constant

    chancancode committed
Commits on Sep 20, 2014
  1. @prathamesh-sonpatki

    Update error message for validate method

    prathamesh-sonpatki committed
    - Improve the error message by suggesting that the user may have
      intended to call validates instead of validate method.
Commits on Aug 18, 2014
  1. Merge pull request #15889 from carnesmedia/model-name

    Rafael Mendonça França committed
    Use #model_name on instances instead of classes
Commits on Aug 5, 2014
  1. @justinweiss

    Add a note on custom validation contexts.

    justinweiss committed
    The documentation on `:on` for validations was inconsistent, and most
    only referenced the `:create` and `:update` contexts. I fixed those to
    be consistent with the documentation on `AM::Validations.validates`,
    which seemed to have the best docs.
    [ci skip]
Commits on Jul 17, 2014
  1. @tenderlove

    %i doesn't work on 1.9

    tenderlove committed
  2. @sonnym

    check for valid options in validate method

    sonnym committed
    This change prevents a certain class of user error which results when
    mistakenly using the `validate` class method instead of the `validates`
    class method.
    Only apply when all arguments are symbols, because some validations use
    the `validate` method and pass in additional options, namely the
    `LenghValidator` via the `ActiveMode::Validations::validates_with`
Commits on Jun 25, 2014
  1. @amiel

    Use #model_name on instances instead of classes

    amiel committed
    This allows rails code to be more confdent when asking for a model name, instead of having to ask for the class.
    Rails core discussion here:!topic/rubyonrails-core/ThSaXw9y1F8
Commits on Mar 27, 2014
  1. @henrik

    ActiveRecord/ActiveModel '#validate' alias for 'valid?'

    henrik committed
    It's unintuitive to call '#valid?' when you want to run validations but
    don't care about the return value.
    The alias in ActiveRecord isn't strictly necessary (the ActiveModel
    alias is still in effect), but it clarifies.
Commits on Jan 27, 2014
  1. @carlosantoniodasilva
  2. @vpuzzella

    Ability to specify multiple contexts when defining a validation.

    vpuzzella committed
    validates_presence_of :name, on: [:update, :custom_validation_context]
Commits on Jan 26, 2014
  1. @fxn

    revises references to :allow_(nil|blank) in some docs [ci skip] [Stev…

    fxn committed
    …en Yang & Xavier Noria]
    Closes #11247.
Commits on Dec 27, 2013
  1. @aditya-kapoor
Commits on Jun 11, 2013
  1. @tenderlove
Commits on Jun 10, 2013
  1. @darai2k
Commits on May 2, 2013
  1. @patricksrobertson

    Convert ActiveModel to 1.9 hash syntax.

    patricksrobertson committed
    I also attempted to fix other styleguide violations such as
    { a: :b } over {a: :b} and foo(b: 'bar') over foo( b: 'bar' ).
Commits on Mar 20, 2013
  1. @Intrepidd

    Tidying up some require : removing useless sort and homogenizing with…

    Intrepidd committed
    … the rest of the code the wat the includes are done
Commits on Mar 19, 2013
  1. @wangjohn

    The repair_validations helper was not working correctly before because

    wangjohn committed
    it only cleared the validations that created :validate callbacks. This
    didn't include the validates created by validates_with, so I've added a
    method to clear all validations.
Commits on Nov 25, 2012
  1. @senny
Commits on Oct 22, 2012
  1. @AvnerCohen
Commits on Oct 21, 2012
  1. @vijaydev

    Merge branch 'master' of

    vijaydev committed
Something went wrong with that request. Please try again.