一直为大量的数组计算影响服务器性能担忧,所以想了一个折中的方法,复杂计算留给浏览器端 JavaScript 计算。具体的步骤大概是这样的:
# udentity和tdentity分别是User和Topic模型中的等长(长度为20)数组field
def get_dentity(topic)
  hover_txt = %(some html tag)
  ri_data = %(data-rateit-udenty="#{current_user.udentity.join(",")}" data-rateit-tdentity="#{topic.tdentity.join(",")}")
raw %(<span class="rateit-hover" style="vertical-align:top;padding-right:5px;">#{hover_txt}</span><div class="rateit" data-rateit-resetable="true" data-rateit-ispreset="true" data-rateit-step="1"  #{ri_data} ></div>)
<span class="pull-right">
  <%= get_dentity(topic) %>
</span>
//set the value if we have it.
if (itemdata('tdentity') && itemdata('udentity')) {
  var tarr, uarr, i, sum, _i;
  tarr = itemdata('tdentity').split(",");
  uarr = itemdata('udentity').split(",");
  sum = 0;
  for (i = _i = 0; _i <= 19; i = ++_i) {
    sum += uarr[i] * tarr[i];
  }
  var score = ((sum / 50) - itemdata('min')) * itemdata('starwidth');
  item.find('.rateit-selected').width(score);
}
else if (itemdata('value')) {
  var score = (itemdata('value') - itemdata('min')) * itemdata('starwidth');
  item.find('.rateit-selected').width(score);
}