开发者

Rails have to create a "fake" column in model

开发者 https://www.devze.com 2023-02-28 05:03 出处:网络
In my tabel I have: 开发者_开发百科 rating_score => The current score ratings => The number of ratings which led to the score

In my tabel I have:

开发者_开发百科
rating_score => The current score
ratings => The number of ratings which led to the score

I want to create a "fake" column named rating which is (rating_score/ratings) in model. That I can use in my controller to order after and in my view.

How should I create this in my model?


Notice that you cannot have the SQL query order your results by a virtual attribute. What you can do is just define the method like this in your model:

def rating
  rating_scope / ratings
end

and order your resultset in ruby based on the value:

results.sort_by(&:rating)
0

精彩评论

暂无评论...
验证码 换一张
取 消