Say I have a set of attributes on my Client model like this:
# firm_size :float
# priority_level :float
# inflection_point :float
# personal_priority :float
# sales_priority :float
# sales_team_priority :float
# days_since_contact :float
# does_client_vote :float
# did_client_vote_for_us :float
# days_until_next_vote :float
# does_client_vote_ii :float
# did_client_vote_ii_for_us :float
# days_until_vote_ii :float
And I need to run a check on each attribute like this:
max = Max.find_or_create_by_user_id(:user_id => current_user.id)
if client.firm.size > max.firm_size
max.firm_size = client.firm.size
end
if client.inflection_point > max.inflection_point
max.inflection_point = client.inflection_point
end
And so on for the rest of the attributes, but this seems quite un-DRY to me.
How do I do this in an elegant way, without having to type out 1 billion if statements for all the attributes?
If you put all the attributes in an array, you could iterate over it and use some metaprogramming to only have to write the logic once: