Syntax Help

Can someone please help me with the following syntax? I’m passing two
arguments to a definition in my model (passing some session info to the
model), and I can’t seem to get the multiple conditions down right.

def self.search(search, user_id)
if search
find(:all, :conditions => [‘firstname LIKE ?’, “#{search}”, ’ AND
user_id == ?’, “#{user_id}”])
else
find(:all, :conditions => [‘user_id == ?’, “#{user_id}”])
end
end

I probably have a comma in there, but is something like this possible?
Two arguments sent to the definition in my model, and using both those
arguments to filter out the records that I find?

Steve C. wrote:

Can someone please help me with the following syntax? I’m passing two
arguments to a definition in my model (passing some session info to the
model), and I can’t seem to get the multiple conditions down right.

def self.search(search, user_id)
if search
find(:all, :conditions => [‘firstname LIKE ?’, “#{search}”, ’ AND
user_id == ?', “#{user_id}”])

Note that “#{var}” is always bad syntax. var.to_s or simply var will
work just as well.

But anyway, your problem is simple. In the array syntax of :conditions,
the strings aren’t paired – rather, the first string is the entire
SQL fragment, and then come all the parameters.

else
  find(:all, :conditions => ['user_id == ?', "#{user_id}"])

Here, you don’t want the array syntax of :conditions. Use the hash
syntax, or find_all_by_user_id.

end

end

I probably have a comma in there, but is something like this possible?
Two arguments sent to the definition in my model, and using both those
arguments to filter out the records that I find?

Yes.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Note that “#{var}” is always bad syntax. var.to_s or simply var will
work just as well.

But anyway, your problem is simple. In the array syntax of :conditions,
the strings aren’t paired – rather, the first string is the entire
SQL fragment, and then come all the parameters.

Gotcha - thanks for the tip. I stripped out the #{var}, and simply used
var and it worked just as well. I got that syntax from Ryan B. over
at Railscasts, but I was watching a pretty old episode so I’m sure
things had changed since then.

Here, you don’t want the array syntax of :conditions. Use the hash
syntax, or find_all_by_user_id.

I had a little trouble initially figuring that one out, but I’m sure I
got it working now. I guess for a beginner syntax of simple things like
arrays and hashes are one of the biggest hurdles to overcome. At least
that’s how it is in my case.

It’s all a learning process though!

Here’s the result; look pretty good?

def self.search(search, user_id)
if search
find(:all, :conditions => [’(firstname LIKE ? OR lastname LIKE ?)
AND user_id == ?’,
search,
search,
user_id
])
else
find(:all, :conditions => { :user_id => user_id })
end
end

Steve C. wrote:
[…]

I stripped out the #{var}, and simply used
var and it worked just as well. I got that syntax from Ryan B. over
at Railscasts, but I was watching a pretty old episode so I’m sure
things had changed since then.

It was never a good syntax, and I’m not sure why Ryan would be using it.
Now, it’s great if you’re doing “#{var} with other stuff”, but it’s
silly if there’s no other stuff.

[…]

Here’s the result; look pretty good?

Mostly.

def self.search(search, user_id)
if search
find(:all, :conditions => [‘(firstname LIKE ? OR lastname LIKE ?)
AND user_id == ?’,
search,
search,
user_id

Why not use named placeholders? That way you don’t have to supply the
same variable twice.

  ])
else
  find(:all, :conditions => { :user_id => user_id })

Again, I’d recommend find_all_by_user_id for something this
straightforward.

end

end

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Why not use named placeholders? That way you don’t have to supply the
same variable twice.

Again, I’d recommend find_all_by_user_id for something this
straightforward.

Thanks a bunch! You’ve motivated me to learn more about named
placeholders, and after doing some research
(http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby)
this page helped me understand how to use them in my particular example.

Now then - how does this look? Maybe it’s because I’m new, but I find
refactoring pretty neat. It’s fun to have code that works, be
simplified into even easier to understand and manage code.

I need to get out of the habit of leaving a problem just because it’s
fixed. Nine times out of ten it could be refactored into better code,
which surely helps me in the long run.

The code:

def self.search(search, user_id)
if search
values = { :search => search, :user_id => user_id }
conditions = “user_id = :user_id AND (firstname LIKE :search OR
lastname LIKE :search)”
find(:all, :conditions => [ conditions, values ])
else
find_all_by_user_id(user_id)
end
end