# Count\_by\_content?

**URL:** <https://www.ruby-forum.com/t/count-by-content/107340>\
**Category:** Ferret\
**Created:** [June 20, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/count-by-content/107340 "2007-06-20T18:19:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [June 20, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/count-by-content/107340/1 "2007-06-20T18:19:42Z")

</div>

Is there a count\_by\_content alternative to the find\_by\_content action?

This is because I’m wanting to do the following in my pagination method:

def list  
# step 1: set the variables you’ll need  
page = (params[:page] ||= 1).to\_i  
items\_per\_page = 20  
offset = (page - 1) \* items\_per\_page

```
# step 2: instead of performing a find, just get a count
item_count = Item.count_with_some_custom_method()

# step 3: create a Paginator, the second argument has to be the

```

number of ALL items on all pages  
@item\_pages = Paginator.new(self, item\_count, items\_per\_page, page)

```
# step 4: only find the requested subset of @items
@items = Item.find_with_some_custom_method(items_per_page, offset)

```

end

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [June 20, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/count-by-content/107340/2 "2007-06-20T21:46:38Z")

</div>

On Wed, Jun 20, 2007 at 06:19:42PM +0200, Richard wrote:

> Is there a count\_by\_content alternative to the find\_by\_content action?

there is a method total\_hits you can call, but this is not really  
necessary. just use find\_by\_contents with the :limit and :offset  
options and call total\_hits on the result returned to find out the total  
number of results.

Jens

> ```
> item_count = Item.count_with_some_custom_method()
> 
> ```
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Ferret-talk mailing list  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/ferret-talk](http://rubyforge.org/mailman/listinfo/ferret-talk)

–  
Jens Krämer  
webit! Gesellschaft für neue Medien mbH  
Schnorrstraße 76 | 01069 Dresden  
Telefon +49 351 46766-0 | Telefax +49 351 46766-66  
removed\_email\_address@domain.invalid | [www.webit.de](http://www.webit.de)

Amtsgericht Dresden | HRB 15422  
GF Sven Haubold, Hagen Malessa
