This project is archived and is in readonly mode.

#2482 ✓duplicate
Nobuhiro IMAI

count with scoped :from option

Reported by Nobuhiro IMAI | April 11th, 2009 @ 07:04 AM | in 2.x

ActiveRecord::Base.count does not care for scoped :from option. This is inconvenient when named_scope with :from option as some kind of sub-query like:

named_scope :top10, :from => "(SELECT * FROM foos ORDER BY foos.rank DESC LIMIT 10) AS foos"

This works fine with find, but count. Here attached the tiny patch with test.

thanks,

Comments and changes to this ticket

  • Nobuhiro IMAI

    Nobuhiro IMAI April 21st, 2009 @ 09:05 AM

    I tryed the following:

    
    class Foo < ActiveRecord::Base
      named_scope :top10, :from => "(SELECT * FROM foos ORDER BY foos.rank DESC LIMIT 10) AS foos" do
        def count(*args)
          options = args.extract_options!
          options[:from] ||= proxy_options[:from]
          args << options
          super(*args)
        end
      end
    end
    

    This works fine with count, but doesn't make sense for (will-)paginate.

    Foo.count => 20 Foo.top10.count => 10 Foo.top10.paginate(:page => 1, :per_page => 5).total_entries => 20

    Is there any other solution to solve this problem?

  • CancelProfileIsBroken

    CancelProfileIsBroken April 22nd, 2009 @ 10:31 PM

    • State changed from “new” to “duplicate”

    Same as #2189 I think

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Referenced by

Pages