开发者

Rails have to create a "fake" column in model

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜