Active Resource - count

How would you do a active resource count, a la

Person.count

I can't see anything like that in the docs.

Cheers Stefan

I think its missing. I see a ticket(without a patch) opened here
http://dev.rubyonrails.org/ticket/8888.
average, sum etc. should also be added, if they don't already exist.

if this is not already done(or in progress by someone else), then I
can help out with a patch.

Thanks,
Jatinder
http://rubymerchant.blogspot.com

How would you do a active resource count, a la

Person.count

I can't see anything like that in the docs.

I don't think that really makes sense to have in ARes. In AR, it
serves an important optimization requirement by allowing you to
perform a SELECT COUNT(*) instead of pulling back all records from the
database. The REST APIs don't offer such a SELECT COUNT(*) idea, so
count is essentially the same as Person.find(:all).size. I think
that's a more honest way to call and get that information. It'll lead
you to think that it's probably a good idea to do:

people = Person.find(:all)
people.size
# other actions on people

We shouldn't pursue API parity with AR for the sake of it. They
operate at very different scales and many practices are not directly
transferable across those scales.

So -1 from here. But it would good to have some documentation to
explain why that is.

The REST APIs don't offer such a SELECT COUNT(*) idea, so
count is essentially the same as Person.find(:all).size.

Well, IMHO the REST APIs ought to offer such an idea.

Typically, I'd expect GET /persons/count.xml to return
<count>47</count> or something similar

We shouldn't pursue API parity with AR for the sake of it. They
operate at very different scales and many practices are not directly
transferable across those scales.

I totally agree with you on this point, however, I believe it would
make sense to add as much functionality as possible from AR to ARes.
This would make the introduction of a middleware layer more feasible
in rails.

Cheers

Stefan

Agreed. This is a perfect example of context over consistency.

-1 over here.

Active Record wraps access to relational databases, Active Resource wraps access to RESTful web services. I expect them to provide a friendly abstraction for the underlying interface which is SQL and HTTP respectively. Counting does not belong to HTTP and in my view whether a resource "/collection/count" makes sense or not is application dependent, so I wouldn't put it in the API.

-- fxn

Counting does not belong to HTTP and in my view
whether aresource"/collection/count" makes sense or not is
application dependent, so I wouldn't put it in the API.

OK, maybe it doesn't make sense to include it in the core API,
however, at the end of the day, I still need that count - transfering
1million records, and then counting, makes no sense.
How would you expose this functionality through a REST interface in
rails? To me it seems like ARes only supports CRUD-style operations -
where you can only get one or more table rows etc.
In addition to count, I also need to call arbitrary business methods
on my active record models - how would you do that? For instance, I'd
like to call stuff like my_user.has_role?('Manager') using a REST api.
Maybe it would make sense to add some functionality similar to XMLRPC
to ARes?
From what I know about REST, functionality and state can both be
considered as resources.

Stefan

You could do something like this:

  # GET /people/count.xml
  def count
    @count = Person.count(:all)

    respond_to do |format|
      format.xml { render :xml => "<?xml version=\"1.0\" encoding=
\"UTF-8\"?>\n<count>#{@count}</count>" }
    end
  end

and then in your routes:
p.resources :people , :collection => { :count => :get }

There's probably a better solution but that should work.

Hi there,

I had a go at this, and managed to get it working. I'll probably
provide a plugin sometime in near future.

Stefan