我正在尝试通过命名范围将参数绑定到联接。但出现错误。
这样做的正确方法是什么?
class Idea < ActiveRecord::Base
#relations
has_many :votes, :inverse_of => :idea
has_one :has_voted, :class_name => 'Vote', :conditions => ['ip = :ip']
# named scopes
scope :with_vote, lambda {|ip| {
:include => [:has_voted],
# like this ??
:conditions => [:has_voted => {:conditions => {:userIp => ip}} ]
}}
end
Idea.with_vote(request.ip).all
我相信我需要模型中的条件定义才能出现在JOIN的ON子句中,而不是出现在WHERE 子句中。
编辑我正在尝试获取以下查询
select Ideas.*, Votes.* from Ideas
left outer join Votes
on Votes.Idea_id = Idea.id AND Votes.ip = {request.ip}