In Ruby/Rails, how can I create one column in a table based on the value of two other columns in the same table? -
i trying create reddit type app order of list depends on combination of number of upvotes link has , created date. plan create new column in "links" table combines "created_date" , "upvotes" "rank value" , sort list "rank value".
is right approach? if so, how create table column using activerecord?
if there meta attribute used purely display purposes, creating method generate on fly appropriate.
if want use sorting objects well, it's better store in column. hopefully, doesn't depend on things current time, , on other attributes:
before_save :calculate_rank def calculate_rank self.rank = self.upvotes + self.clicks * 5; end
unfortunately, use case said column depends on creation date, in terms of "how fresh it" -- moving target.
you can solve 2 ways: increasing rank
values newer links indefinitely, or putting items time buckets , updating them periodically (degrading scores when day or week ends, perhaps).
Comments
Post a Comment