# Ccreating methods on the fly / counting distinct field values

**URL:** <https://www.ruby-forum.com/t/ccreating-methods-on-the-fly-counting-distinct-field-values/149192>\
**Category:** Rails\
**Created:** [September 22, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/ccreating-methods-on-the-fly-counting-distinct-field-values/149192 "2008-09-22T13:11:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat.turner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47774/32.png) [@pat.turner](https://www.ruby-forum.com/u/pat.turner)\
**Post date:** [September 22, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/ccreating-methods-on-the-fly-counting-distinct-field-values/149192/1 "2008-09-22T13:11:51Z")

</div>

Hi,

I’m pretty new to ruby and rails, so apologies if this has been covered  
before (I couldn’t find a thread on it).

I want to retrieve a count of records held in the DB for every distinct  
value of an attribute.

E.g. given a class Address with a postcode field, I would like to  
retrieve  
an array of ValueCount objects. The ValueCount object would hold the  
value  
retrieved from the db and the number of records which hold it.

I’ve written a class specific method which does this:  
selects all occurrences of an object with  
ActiveRecord.find(:all, :select =\> “DISTINCT postcode”)  
then for each value issues a  
ActiveRecord.count(:all, :conditions =\> {:postcode =\> postcode}) call.

I realise this is rather clumsy and could probably be done in a better  
way,  
but it does work.

However, my problem is that I want to write a method to create this sort  
of  
call on the fly.  
E.g.  
distinct\_count :postcode  
would create a distinct\_count\_postcode method which achieves all of the  
above for the postcode field. I can then call this in any of my model  
classes.

So, can anyone point me in the direction of a tutorial for creating  
methods  
on the fly using symbols?

If anyone can show a better way to perform the ActiveRecord value\_count  
logic, that would be great too 🙂

Thanks,  
Pat

–  
View this message in context:  
[http://www.nabble.com/Ccreating-methods-on-the-fly---counting-distinct-field-values-tp19605876p19605876.html](http://www.nabble.com/Ccreating-methods-on-the-fly---counting-distinct-field-values-tp19605876p19605876.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![pat.turner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47774/32.png) [@pat.turner](https://www.ruby-forum.com/u/pat.turner)\
**Post date:** [September 22, 2008, 1:15pm UTC](https://www.ruby-forum.com/t/ccreating-methods-on-the-fly-counting-distinct-field-values/149192/2 "2008-09-22T13:15:58Z")

</div>

On Sep 22, 7:11 pm, “pat.turner” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, my problem is that I want to write a method to create this sort of  
> call on the fly.  
> E.g.  
> distinct\_count :postcode  
> would create a distinct\_count\_postcode method which achieves all of the  
> above for the postcode field. I can then call this in any of my model  
> classes.
> 
> So, can anyone point me in the direction of a tutorial for creating methods  
> on the fly using symbols?

Look for tutorials dealing with mixins or plugins.
