dynamic attribute for a rails model -
dynamic attribute for a rails model -
i'm wondering best way accomplish want :p
i have model 'modela' has many 'modelarate'. modelarate contains comment or rate (or both).
i don't want calculate average rate each time modela shown. because it's not cool database (accessing modelarate , getting rate ...) need know lowest , highest rate.
how best way accomplish in rails ? (without using gem, need understand how works)
i thinking of adding attribute rate_score in modela increasing when modelarate created, , method homecoming rate_score / modelarates.size lowest , highest rate, attribute ? there no mechanism can me in rails ?
thanks help
you can utilize calculation method on rails http://api.rubyonrails.org/classes/activerecord/calculations.html
for models, given modela record, can
maximum = model_a.model_a_rates.maximum(:rate) minimum = model_a.model_a_rates.minimum(:rate) average = model_a.model_a_rates.average(:rate)
all of these calculations done on sql server fast there's no need worry speed.
ruby-on-rails ruby-on-rails-3 database-design model attributes
Comments
Post a Comment