我想知道如何在postgress数据库的rails查询中写这个ruby?它通过mysql工作,但在postgres上它返回一个错误:
RUBY ON RAILS QUERY
response_array = Vote.
by_leaderboard_date_range.
joins(:user).
joins(:event_poll_relationship).
select('users.*, sum(points_earned) as total').
group('user_id').
order('total DESC').
limit(20)
错误
2013-07-31T13:02:43.430333+00:00 app[web.3]: ActiveRecord::StatementInvalid (PG::Error: ERROR: argument of JOIN/ON must be type boolean, not type integer
2013-07-31T13:02:43.430333+00:00 app[web.3]: LINE 1: ...ers"."id" = "votes"."user_id" LEFT JOIN events on events.esp...
2013-07-31T13:02:43.430333+00:00 app[web.3]: ^
2013-07-31T13:02:43.430333+00:00 app[web.3]: : SELECT users.*, sum(points_earned) as total FROM "votes" INNER JOIN "event_poll_relationships" ON "event_poll_relationships"."id" = "votes"."event_poll_relationship_id" INNER JOIN "users" ON "users"."id" = "votes"."user_id" LEFT JOIN events on events.espn_event_id WHERE (events.espn_event_id = event_poll_relationships.espn_event_id AND events.start_date_time >= '2013-07-28 00:00:00' AND events.start_date_time <= '2013-12-31 00:00:00') GROUP BY user_id ORDER BY total DESC LIMIT 20):
投票模型
class Vote < ActiveRecord::Base
scope :by_espn_event_id, lambda {|espn_event_id| joins(:event_poll_relationship).joins('LEFT JOIN events on events.espn_event_id').where('event_poll_relationships.espn_event_id = ? AND events.espn_event_id = event_poll_relationships.espn_event_id', espn_event_id) }
scope :by_leaderboard_date_range, joins(:event_poll_relationship).joins('LEFT JOIN events on events.espn_event_id').where('events.espn_event_id = event_poll_relationships.espn_event_id AND events.start_date_time >= ? AND events.start_date_time <= ?',LeaderBoardRange.find(1).start_date.to_datetime,LeaderBoardRange.find(1).end_date.to_datetime)
RAW GENERATED SQL
SELECT users.*, sum(points_earned) as total FROM "votes" INNER JOIN "event_poll_relationships" ON "event_poll_relationships"."id" = "votes"."event_poll_relationship_id" INNER JOIN "users" ON "users"."id" = "votes"."user_id" LEFT JOIN events on events.espn_event_id WHERE (events.espn_event_id = event_poll_relationships.espn_event_id AND events.start_date_time >= '2013-07-28 00:00:00' AND events.start_date_time <= '2013-12-31 00:00:00') GROUP BY user_id ORDER BY total DESC LIMIT 20)
由于
答案 0 :(得分:1)
<强>解强>
问题出在范围内的JOIN ON上。这适用于dbs:mysql和postgres
scope :by_espn_event_id, lambda {|espn_event_id| joins(:event_poll_relationship).joins('LEFT JOIN events ON (events.espn_event_id = event_poll_relationships.espn_event_id)').where('event_poll_relationships.espn_event_id = ?', espn_event_id) }
scope :by_leaderboard_date_range, joins(:event_poll_relationship).joins('LEFT JOIN events ON (events.espn_event_id = event_poll_relationships.espn_event_id)').where('events.start_date_time >= ? AND events.start_date_time <= ?',LeaderBoardRange.find(1).start_date.to_datetime,LeaderBoardRange.find(1).end_date.to_datetime)
RUBY ON RAILS QUERY
# calculate leaderboard by date range
response_array = Vote.
by_leaderboard_date_range.
joins(:user).
joins(:event_poll_relationship).
select('users.*, sum(points_earned) as total').
group('users.id, user_id').
order('total DESC').
limit(20)