Lisa
Lisa

Reputation: 27

ruby on rails, nested attributes, take highest attribute

I have a model called degree.rb that belongs_to :user (inside user.rb - has_many :degrees

Let's say a user has 6 degrees. In the degrees table, there is a column called degree_type that holds an int. What is the best way to pull all 6 degrees and determine the highest degree_type number in existence for that user?

It doesn't matter which entry is highest or even if there are duplicates. I just need to know what highest entry for degree_type for a specific user exists.

Any suggs? Thanks!

Upvotes: 2

Views: 99

Answers (1)

Maurício Linhares
Maurício Linhares

Reputation: 40333

Use maximum:

user.degrees.maximum( :degree_type )

Upvotes: 1

Related Questions