Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

count with scoped :from option

#2482

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,

Reported by Nobuhiro IMAI · April 11th, 2009 @ 07:04 AM

State: duplicate
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Nobuhiro IMAI
    Nobuhiro IMAI

    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?

    April 21st, 2009 @ 09:05 AM

  2. CancelProfileIsBroken