1. 05 1月, 2013 2 次提交
  2. 30 11月, 2012 1 次提交
    • J
      Use separate Relation subclasses for each AR class · 64c53d7c
      Jon Leighton 提交于
      At present, ActiveRecord::Delegation compiles delegation methods on a
      global basis. The compiled methods apply to all subsequent Relation
      instances. This creates several problems:
      
      1) After Post.all.recent has been called, User.all.respond_to?(:recent)
         will be true, even if User.all.recent will actually raise an error due
         to no User.recent method existing. (See #8080.)
      
      2) Depending on the AR class, the delegation should do different things.
         For example, if a Post.zip method exists, then Post.all.zip should call
         it. But this will then result in User.zip being called by a subsequent
         User.all.zip, even if User.zip does not exist, when in fact
         User.all.zip should call User.all.to_a.zip. (There are various
         variants of this problem.)
      
      We are creating these compiled delegations in order to avoid method
      missing and to avoid repeating logic on each invocation.
      
      One way of handling these issues is to add additional checks in various
      places to ensure we're doing the "right thing". However, this makes the
      compiled methods signficantly slower. In which case, there's almost no
      point in avoiding method_missing at all. (See #8127 for a proposed
      solution which takes this approach.)
      
      This is an alternative approach which involves creating a subclass of
      ActiveRecord::Relation for each AR class represented. So, with this
      patch, Post.all.class != User.all.class. This means that the delegations
      are compiled for and only apply to a single AR class. A compiled method
      for Post.all will not be invoked from User.all.
      
      This solves the above issues without incurring significant performance
      penalties. It's designed to be relatively seamless, however the downside
      is a bit of complexity and potentially confusion for a user who thinks
      that Post.all and User.all should be instances of the same class.
      
      Benchmark
      ---------
      
      require 'active_record'
      require 'benchmark/ips'
      
      class Post < ActiveRecord::Base
        establish_connection adapter: 'sqlite3', database: ':memory:'
        connection.create_table :posts
      
        def self.omg
          :omg
        end
      end
      
      relation = Post.all
      
      Benchmark.ips do |r|
        r.report('delegation')   { relation.omg }
        r.report('constructing') { Post.all }
      end
      
      Before
      ------
      
      Calculating -------------------------------------
                delegation      4392 i/100ms
              constructing      4780 i/100ms
      -------------------------------------------------
                delegation   144235.9 (±27.7%) i/s -     663192 in   5.038075s
              constructing   182015.5 (±21.2%) i/s -     850840 in   5.005364s
      
      After
      -----
      
      Calculating -------------------------------------
                delegation      6677 i/100ms
              constructing      6260 i/100ms
      -------------------------------------------------
                delegation   166828.2 (±34.2%) i/s -     754501 in   5.001430s
              constructing   116575.5 (±18.6%) i/s -     563400 in   5.036690s
      
      Comments
      --------
      
      Bear in mind that the standard deviations in the above are huge, so we
      can't compare the numbers too directly. However, we can conclude that
      Relation construction has become a little slower (as we'd expect), but
      not by a huge huge amount, and we can still construct a large number of
      Relations quite quickly.
      64c53d7c
  3. 19 11月, 2012 1 次提交
  4. 18 11月, 2012 1 次提交
  5. 29 10月, 2012 1 次提交
  6. 19 10月, 2012 2 次提交
  7. 12 10月, 2012 1 次提交
  8. 10 10月, 2012 1 次提交
  9. 24 8月, 2012 1 次提交
  10. 19 8月, 2012 1 次提交
  11. 17 8月, 2012 1 次提交
    • E
      Fix merge error when Equality LHS is non-attribute · b127d86c
      Ernie Miller 提交于
      This is at best a band-aid for a more proper fix, since it won't truly
      handle the removal of the previous equality condition of these other
      nodes. I'm planning to put in some work on ARel toward supporting that
      goal.
      
      Related: rails/arel#130, ernie/squeel#153, ernie/squeel#156
      b127d86c
  12. 15 8月, 2012 1 次提交
  13. 03 8月, 2012 1 次提交
    • J
      Remove ActiveRecord::Base.to_a · 55b24888
      Jon Leighton 提交于
      On reflection, it seems like a bit of a weird method to have on
      ActiveRecord::Base, and it shouldn't be needed most of the time anyway.
      55b24888
  14. 02 8月, 2012 1 次提交
    • J
      Add `Relation#load` · 437851ea
      Jon Leighton 提交于
      This method explicitly loads the records and then returns `self`.
      
      Rather than deciding between "do I want an array or a relation?",
      most people are actually asking themselves "do I want to eager load
      or lazy load?" Therefore, this method provides a way to explicitly
      eager-load without having to switch from a `Relation` to an array.
      
      Example:
      
          @posts = Post.where(published: true).load
      437851ea
  15. 31 7月, 2012 1 次提交
  16. 28 7月, 2012 1 次提交
  17. 27 7月, 2012 1 次提交
    • J
      ActiveRecord::Base.all returns a Relation. · 6a81ccd6
      Jon Leighton 提交于
      Previously it returned an Array.
      
      If you want an array, call e.g. `Post.to_a` rather than `Post.all`. This
      is more explicit.
      
      In most cases this should not break existing code, since
      Relations use method_missing to delegate unknown methods to #to_a
      anyway.
      6a81ccd6
  18. 13 7月, 2012 1 次提交
  19. 07 7月, 2012 2 次提交
  20. 06 7月, 2012 1 次提交
    • J
      Show the records in Relation#inspect · 07314e64
      Jon Leighton 提交于
      The reason for removing the previous implementation of `#inspect` was
      that it hid from you that you were dealing with a `Relation` rather than
      an `Array`.
      
      But it is still useful to be able to see the records, particularly if you're
      writing something like the following in tests:
      
          assert_equal [foo], Post.where(:bar)
      
      If the assertion fails, you want to see what records were actually
      loaded.
      
      So this implementation makes it clear that you've got a `Relation`, but
      also shows your records.
      07314e64
  21. 22 6月, 2012 1 次提交
  22. 03 6月, 2012 1 次提交
  23. 01 6月, 2012 1 次提交
  24. 21 5月, 2012 1 次提交
  25. 18 5月, 2012 2 次提交
  26. 17 5月, 2012 1 次提交
  27. 11 5月, 2012 1 次提交
  28. 06 5月, 2012 1 次提交
  29. 05 5月, 2012 1 次提交
  30. 04 5月, 2012 1 次提交
  31. 30 4月, 2012 1 次提交
  32. 27 4月, 2012 3 次提交
  33. 26 4月, 2012 2 次提交