0

I'm building a custom profile completeness tool in Rails 3.1. Using active_admin, the administrator wants to be able to pick model attributes and assign a score value to them. I create a table with 'name' and 'score' in it. The name being the column name to apply the score to.

I need to compare the values from the name column in the scoring table when the model gets updated. Psuedocode:

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]
}

Of course other approaches are welcome! I looked at completeness-fu but it is way out of date.

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 に答える