# Why can't I 'count' the results from a model 'find'?

**URL:** <https://www.ruby-forum.com/t/why-cant-i-count-the-results-from-a-model-find/154157>\
**Category:** Rails\
**Created:** [November 25, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/why-cant-i-count-the-results-from-a-model-find/154157 "2008-11-25T23:40:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [November 25, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/why-cant-i-count-the-results-from-a-model-find/154157/1 "2008-11-25T23:40:41Z")

</div>

Hi,

I’m running the following code in my show() method:

```
    @articles = Article.find(:all, :conditions => ["issue_id = ?",

```

@issue.id], :order =\> ‘`column`,position’)

```
    if (params[:article_id])
      @article_id = params[:article_id].to_i
      c = 0
      total = @articles.count
      while (c < total)
        if @article_id == @articles[c].id
          if c == 0
            @article_prev = @articles.last.id
          else
            @article_prev = @articles[(c-1)].id
          end
          if ((c+1) == total)
            @article_next = @articles.first.id
          else
            @article_next = @articles[(c+1)].id
          end
        end
      end
    else
      @article_id = 0
      @article_main = @articles.first.id
      @article_prev = @articles.last.id
      @article_next = @articles[1].id
    end

```

Now to be honest this code is already a bit odd for me but I have no  
idea how else to approach this in Ruby on Rails. So I’ve attempted to do  
it PHP-style. But I’m being told that I can’t do @articles.count

Why? I have a user model and a user has a one to many relationship with  
articles so I know that: @user.articles.count works. Hence I’m guessing  
there’s something big I’m missing here.

So, how am I going to do this? How do I easily cycle through my list of  
articles and get the previous and next ones?

Any help greatly appreciated. I’ve been going back and forth over my  
books and forums but I can’t seem to formulate the question to be able  
to ask it.

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [November 26, 2008, 12:11am UTC](https://www.ruby-forum.com/t/why-cant-i-count-the-results-from-a-model-find/154157/2 "2008-11-26T00:11:53Z")

</div>

On Nov 25, 2008, at 2:40 PM, Theo Graham-brown wrote:

> ```
> c = 0
> total = @articles.count
> 
> ```

…

> guessing  
> there’s something big I’m missing here.
> 
> So, how am I going to do this? How do I easily cycle through my list  
> of  
> articles and get the previous and next ones?

@user.articles.count is Rails magic. It’s not actually fetching  
everything and then counting it up, it’s looking at the associations  
and building a “SELECT COUNT…”

@articles above is an array. Try @articles.size.

-philip

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [November 26, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/why-cant-i-count-the-results-from-a-model-find/154157/3 "2008-11-26T19:17:41Z")

</div>

On Nov 25, 11:10 pm, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Nov 25, 2008, at 2:40 PM, Theo Graham-brown wrote:
> 
> @user.articles.count is Rails magic. It’s not actually fetching  
> everything and then counting it up, it’s looking at the associations  
> and building a “SELECT COUNT…”

To be quite precise, @user.articles is not an array, it’s an  
association proxy.

Fred

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [November 27, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/why-cant-i-count-the-results-from-a-model-find/154157/4 "2008-11-27T17:13:44Z")

</div>

Thanks both of you.

I’ve got it working now. 🙂
