0

我正在 Rails 3.1 中构建自定义配置文件完整性工具。使用 active_admin,管理员希望能够选择模型属性并为其分配分值。我创建了一个包含“名称”和“分数”的表。name 是要应用分数的列名。

当模型更新时,我需要比较评分表中名称列的值。伪代码:

ProfileScore.find(:all, :select => "name,score").inject { |p,score|
  #this is where im stuck, 'self' == updated model hash
  p[:name] == self.attribute.updated
  score += p[:score]
}

当然也欢迎其他方法!我查看了完整性,但它已经过时了。

4

2 回答 2

1
score = ProfileScore.select("name, score").inject(0) { |memo, pscore|
  # the result is a list of ProfileScore objects, not of pairs.

  # if p[:name] == self.attribute.updated # don't you want to take into account
  # also old, but present, attributes??
  if self.attributes[p[:name]].present?
    memo += p[:score]
  end
  memo
}

或者也

present_attributes = self.attributes.reject { |k,v| v.blank? }.keys
score = ProfileScore.where(:name => present_attributes).sum("score")
于 2012-10-02T23:28:35.767 回答
0

要获得总分,您可以使用:

total_score = ProfileScore.where(:name => self.attribute.updated).sum(:score)
于 2012-10-02T23:00:49.230 回答