Reputation: 1276
I've been fighting with this one for a little while. This gets me all of the counts for each zip code...
Customer.group(:zip).count
But I can't get it to order by the ones with the highest count. Seems like this should be simple but I can't seem to find anything that addresses this case.
UPDATE: Getting the most popular cities and states would probably be even more helpful. It seems to me ActiveRecord really isn't suited for this, as i'm not really asking for a Cutomer model, but I can't find anything about querying outside of ActiveRecord. If anyone can point me to any resources about interacting with the database in cases where I don't need the data to fit into a model object, that would be very helpful.
Upvotes: 0
Views: 37
Reputation: 479
You can try follow syntax, it will return result with the highest count:
Customer.group(:zip).order("count_zip desc").count(:zip)
Upvotes: 1
Reputation: 2470
If I understand what you want. you can try any of the following below
Customer.where(zip: 'testcode').maximum("value")
or you could try this
Customer.where(zip: 'testcode').order("value DESC").first
or
Customer.where(zip: :testcode).order(value: :desc).first
you also have ruby documentation at your rescue link
Upvotes: 0