每首诗作为两个投票,一个作为poem_id,other_poem_id,胜出&第二个记录是第一个记录的倒数。也许有更好的方法,但我试图找到一段时间内获胜率最高的诗歌。由于每次比较的双重记录,这令人困惑。我应该添加另一个表,结果,它有两个投票记录的comparison_id吗?
Here is a sample
poem_id:1 other_poem_id:2 wins:3
poem_id:2 other_poem_id:1 wins:3
so it is 50% rather than a running tally
scope :recent, lambda {
{ :joins => "JOIN votes ON votes.poem_id = poems.id",
:conditions => ["poems.created_at > ?", 8.days.ago],
:order => "votes.wins DESC",
:limit => 10
}
}
的ActiveRecord :: StatementInvalid: SQLite3 :: SQLException:含糊不清 列名:created_at:SELECT
“诗歌”。*来自“诗歌”加入 投票票数.poem_id = poems.id WHERE(created_at>'2010-02-12 15:12:35.764252')ORDER BY赢得DESC 限制10
编辑:我改变了架构,这是我现在正在使用的...
以下是一个跟踪诗歌排名的模型。我昨天刚刚写了第一稿。它看起来有点笨重,但我还不知道如何改进它。 DailyRanking.tabulate将每晚由cron调用。 (在模型之后是比较的模式。)
# == Schema Information
# Schema version: 20100221120442
#
# Table name: daily_rankings
#
# id :integer not null, primary key
# poem_id :integer
# rank :integer
# percentile :integer
# wins :integer
# losses :integer
# draws :integer
# comparisons :integer
# created_at :datetime
# updated_at :datetime
#
class DailyRanking < ActiveRecord::Base
belongs_to :poem
class << self
def tabulate
# 1. get all comparisons over the past 24 hours
comparisons = Comparison.day.all
# 2. collect poem id for each time it wins
# TODO make hash of "poem_id" => {:wins => a, :losses => b, :draws => c}
a, results = 0, []
while a < comparisons.size
c = comparisons[a]
if c.poem1_id == c.winner_id
results << c.poem1_id
elsif c.poem2_id == c.winner_id
results << c.poem2_id
end
a += 1
end
# 3. presort by poem count
a, unsorted_wins = 0, []
until results.empty?
unsorted_wins << [results.first, results.count(results.first)]
results.delete(results.first)
end
# 4. sort by win count
sorted_wins = unsorted_wins.sort { |a, b| b[1] <=> a[1] }
# 5. repeat for losses
a, results = 0, []
while a < comparisons.size
c = comparisons[a]
if c.poem1_id == c.loser_id
results << c.poem1_id
elsif c.poem2_id == c.loser_id
results << c.poem2_id
end
a += 1
end
unsorted_losses = []
until results.empty?
unsorted_losses << [results.first, results.count(results.first)]
results.delete(results.first)
end
sorted_losses = unsorted_losses.sort { |a, b| b[1] <=> a[1] }
# 6. sort wins v losses
# a. sort wins[poem] v losses[poem]
# b. get poem and pct wins for wins[poem]
# c. delete wins[poem] and losses[poem]
# repeat
unsorted_results, a = [], 0
while a < sorted_wins.size
poem_id = sorted_wins[a][0]
wins = sorted_wins[a][1]
losses = sorted_losses.select do |item|
item.second if item.first == poem_id
end.compact.first.second
unsorted_results << [ poem_id, wins / (wins + losses).to_f ]
a += 1
end
# 7. sort by pct
sorted_results = unsorted_results.sort { |a, b| b[1] <=> a[1] }
# 8. persist rankings
sorted_results.each_with_index do |result, index|
ranking = find_or_create_by_rank(index + 1)
ranking.poem_id = result.first
ranking.save!
end
end
end
end
# == Schema Information
# Schema version: 20100221120442
#
# Table name: comparisons
#
# id :integer not null, primary key
# poem1_id :integer
# poem2_id :integer
# response :string(4) default("none"), not null
# winner_id :integer
# loser_id :integer
# user_id :integer
# session_id :integer
# ip :string(15)
# created_at :datetime
# updated_at :datetime
#
class Comparison < ActiveRecord::Base
scope :day, lambda { { :conditions => ["created_at > ?", 1.day.ago] } }
end
答案 0 :(得分:0)
我认为适用于您的SQL查询类似于SELECT poems.*, percentage as ((SELECT wins FROM votes WHERE poem_id = poem.id WHERE created_at > 8.days.ago) / (SELECT wins FROM votes WHERE other_poem_id = poem.id WHERE created_at > 8.days.ago)) ORDER BY percentage DESC LIMIT 10
。至于如何优化并将其转换为Rails范围,我不确定。
但是,您遇到的错误是由["poems.created_at > ?", 8.days.ago]
条件转换为SQL的方式引起的。 SQLite不知道你是在寻找poems.created_at还是votes.created_at(顺便说一句,从你的描述来看,我认为你想要votes.created_at
)。