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)
精彩评论