1. 15 3月, 2013 1 次提交
    • Y
      rename `Relation#uniq` to `Relation#distinct`. `#uniq` still works. · a1bb6c8b
      Yves Senn 提交于
      The similarity of `Relation#uniq` to `Array#uniq` is confusing. Since our
      Relation API is close to SQL terms I renamed `#uniq` to `#distinct`.
      
      There is no deprecation. `#uniq` and `#uniq!` are aliases and will continue
      to work. I also updated the documentation to promote the use of `#distinct`.
      a1bb6c8b
  2. 02 3月, 2013 1 次提交
    • Y
      deal with `#append` and `#prepend` on association collections. · b9399c47
      Yves Senn 提交于
      Closes #7364.
      
      Collection associations behave similar to Arrays. However there is no
      way to prepend records. And to append one should use `<<`. Before this
      patch `#append` and `#prepend` did not add the record to the loaded
      association.
      
      `#append` now behaves like `<<` and `#prepend` is not defined.
      b9399c47
  3. 01 3月, 2013 1 次提交
  4. 18 1月, 2013 1 次提交
  5. 07 1月, 2013 1 次提交
  6. 05 12月, 2012 1 次提交
    • C
      Replace comments' non-breaking spaces with spaces · 019df988
      claudiob 提交于
      Sometimes, on Mac OS X, programmers accidentally press Option+Space
      rather than just Space and don’t see the difference. The problem is
      that Option+Space writes a non-breaking space (0XA0) rather than a
      normal space (0x20).
      
      This commit removes all the non-breaking spaces inadvertently
      introduced in the comments of the code.
      019df988
  7. 02 12月, 2012 1 次提交
  8. 30 11月, 2012 3 次提交
    • 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
    • F
    • F
      add documentation to CollectionProxy#empty? · 61a7a9f3
      Francesco Rodriguez 提交于
      61a7a9f3
  9. 09 11月, 2012 5 次提交
    • J
      fix warnings · 1489303d
      Jon Leighton 提交于
      1489303d
    • J
      Delegate all calculations to the scope. · edd94cee
      Jon Leighton 提交于
      So that the scope may be a NullRelation and return a result without
      executing a query.
      
      Fixes #7928
      edd94cee
    • J
      CollectionProxy#pluck issues no query for a new_record? owner · 11b846ee
      Jon Leighton 提交于
      Fixes #8102.
      
      I couldn't find a nicer way to deal with this than delegate the call to
       #scope, which will be a NullRelation when we want it to be.
      11b846ee
    • J
      Nullify the relation at a more general level. · aae4f357
      Jon Leighton 提交于
      This allows us to avoid hacks like the "return 0 if owner.new_record?"
      in #count (which this commit removes).
      
      Also, the relevant foreign key may actually be present even on a new
      owner record, in which case we *don't* want a null relation. This logic
      is encapsulated in the #null_scope? method.
      
      We also need to make sure that the CollectionProxy is not 'infected'
      with the NullRelation module, or else the methods from there will
      override the definitions in CollectionProxy, leading to incorrect
      results. Hence the nullify: false option to CollectionAssociation#scope.
      (This feels a bit nasty but I can't think of a better way.)
      aae4f357
    • J
      Relations built off collection associations with an unsaved owner should be null relations · 0130c174
      Jon Leighton 提交于
      For example, the following should not run any query on the database:
      
      Post.new.comments.where(body: 'omg').to_a # => []
      
      Fixes #5215.
      0130c174
  10. 22 9月, 2012 2 次提交
  11. 17 9月, 2012 2 次提交
  12. 13 9月, 2012 1 次提交
  13. 02 8月, 2012 3 次提交
    • J
      s/scoped/scope/ · b33e7ba1
      Jon Leighton 提交于
      b33e7ba1
    • J
      Add CollectionProxy#scope · 0e1cafcb
      Jon Leighton 提交于
      This can be used to get a Relation from an association.
      
      Previously we had a #scoped method, but we're deprecating that for
      AR::Base, so it doesn't make sense to have it here.
      
      This was requested by DHH, to facilitate code like this:
      
          Project.scope.order('created_at DESC').page(current_page).tagged_with(@tag).limit(5).scoping do
            @topics      = @project.topics.scope
            @todolists   = @project.todolists.scope
            @attachments = @project.attachments.scope
            @documents   = @project.documents.scope
          end
      0e1cafcb
    • J
      Use explicit delegations · 26d3e325
      Jon Leighton 提交于
      This makes it easier to see what the documentation refers to.
      
      It also means that we are not doing unnecessary work for delegations
      that have no args / splats / block / etc.
      26d3e325
  14. 31 7月, 2012 2 次提交
  15. 28 7月, 2012 1 次提交
  16. 29 5月, 2012 1 次提交
    • F
      Add support for CollectionAssociation#delete by Fixnum or String · 39f06984
      Francesco Rodriguez 提交于
      I found the next issue between CollectionAssociation `delete`
      and `destroy`.
      
          class Person < ActiveRecord::Base
            has_many :pets
          end
      
          person.pets.destroy(1)
          # => OK, returns the destroyed object
      
          person.pets.destroy("2")
          # => OK, returns the destroyed object
      
          person.pets.delete(1)
          # => ActiveRecord::AssociationTypeMismatch
      
          person.pets.delete("2")
          # => ActiveRecord::AssociationTypeMismatch
      
      Adding support for deleting with a fixnum or string like
      `destroy` method.
      39f06984
  17. 28 5月, 2012 1 次提交
  18. 27 5月, 2012 1 次提交
  19. 26 5月, 2012 5 次提交
  20. 24 5月, 2012 1 次提交
  21. 23 5月, 2012 5 次提交