How to find the minimum value in a table column in Rails 3

Hi, let's say I have a table (declarations) with a column (representations)

Kinds
2
one
4
6
3

How to find the smallest value in this column? Any easy way to do this?


This is what I have
@ads = Ad.all @show_this_ad = @ads.min(:views) 

this gives me "the wrong number of arguments (1 for 0)" "


 @ads = Ad.all @show_this_ad = @ads.minimum(:views) 

this gives me an undefined method error "

+7
source share
1 answer
 Ad.minimum(:views) 

must work

You can add additional restrictions, for example:

 Ad.where(:user_id => 12345).minimum(:views) 

To find only add by user with id 12345

btw: You can easily test such things in the rails console (just type "rails c" from the command line) One thing that often helps me is simply to get the result class of some operation.

If you enter something like:

 @foo = Add.all 

And then:

 @foo.class 

You will see that @foo is an array that, of course, knows nothing about ActiveRecord # minimum

+20
source

All Articles