pass parameter from controller to models condition
Posted
by
Alex
on Stack Overflow
See other posts from Stack Overflow
or by Alex
Published on 2012-06-02T16:13:06Z
Indexed on
2012/06/02
16:41 UTC
Read the original article
Hit count: 169
ruby-on-rails
|activerecord
I'm trying to bind a param to a join via a named scope., but I'm getting an error.
What is the correct way to do that?
has_one :has_voted, :class_name => 'Vote', :conditions => ['ip = :userIp']
# named scopes
scope :with_vote, lambda {|ip| {
:include => [:has_voted],
# like this ??
:conditions => [:has_voted => {:conditions => {:userIp => ip}} ]
}}
Idea.with_vote(request.ip).all
I believe I need the condition definition in the model for it to appear in the ON clause of a JOIN, rather then in the WHERE one.
Edit I'm trying to get the following query
select Ideas.*, Votes.* from Ideas
left outer join Votes
on Votes.Idea_id = Idea.id AND Votes.ip = {request.ip}
© Stack Overflow or respective owner