如何在switchcase中使用write属性应该是一个整数

时间:2016-06-03 05:21:05

标签: ruby-on-rails

我有一个带有问题和答案的rails应用程序,每个答案都有一个等级取决于某些参数,如示例 注意:我不想要从1到10的星号方面,所以我可以稍后对所有字段进行加权平均并进行计算。 如何使用write属性来获取数据? 错误:SystemStackError:堆栈级别太深

def set_price_rating
  # set_price = 5
  puts case price_today
  when 1..21 then
    "0".to_i
  when 22..29 then
    "5".to_i
  when 30..39 then
    "7".to_i
  when 40..49 then
    "8".to_i
  when 50..59 then
    "6".to_i
  else
    "0".to_i
  end
  write_attribute(:price_rating, set_price_rating)
  save
end

1 个答案:

答案 0 :(得分:0)

您可以尝试自定义以下相关宝石:

https://github.com/wazery/ratyrate

或请花一些时间来理解这里的概念:

https://www.sitepoint.com/ratyrate-add-rating-rails-app/