Can :conditions use an array and a dictionary together?

3 views
Skip to first unread message

CoolAJ86

unread,
Aug 20, 2009, 3:01:00 AM8/20/09
to Ruby on Rails: Talk
This is what I'm trying to do:

@contacts = Contact.find(:all, :conditions => [["jobs.name LIKE ?",
'Teacher%'], {:group_id => current_contact.group}], :include =>
[:jobs, :photos])

Obviously it's not working. Is this my only option?
@contacts = Contact.find(:all, :conditions => ['jobs.name LIKE ? AND
group_id = ?', 'Teacher%', current_contact.group], :include =>
[:jobs, :photos])

Jeffrey L. Taylor

unread,
Aug 20, 2009, 4:39:32 AM8/20/09
to rubyonra...@googlegroups.com

:conditions =>
"jobs.name LIKE 'Teacher%' AND group_id = #{current_contact.group}"

HTH,
Jeffrey

Frederick Cheung

unread,
Aug 20, 2009, 4:46:14 AM8/20/09
to Ruby on Rails: Talk
In general that is about it. In this particular case it looks like you
could do

current_contact.group.contacts.find :all, :conditions => [["jobs.name
LIKE ?", 'Teacher%'], ...

assuming that group has_many contacts

Fred

Sijo Kg

unread,
Aug 20, 2009, 6:28:06 AM8/20/09
to rubyonra...@googlegroups.com
Jeffrey L. Taylor wrote:

> Quoting CoolAJ86 <cool...@gmail.com>:
>> [:jobs, :photos])
> :conditions =>
> "jobs.name LIKE 'Teacher%' AND group_id = #{current_contact.group}"
>
> HTH,
> Jeffrey

Passing a string to :conditions like this welcomes sql injection attacks
So can it be avoided and pass an array instaed like in last post by
fred?Am I right?


Sijo
--
Posted via http://www.ruby-forum.com/.

Jeffrey L. Taylor

unread,
Aug 20, 2009, 10:29:25 AM8/20/09
to rubyonra...@googlegroups.com
Quoting Sijo Kg <rails-mai...@andreas-s.net>:
>
> Jeffrey L. Taylor wrote:
> > Quoting CoolAJ86 <cool...@gmail.com>:
> >> [:jobs, :photos])
> > :conditions =>
> > "jobs.name LIKE 'Teacher%' AND group_id = #{current_contact.group}"
> >
> > HTH,
> > Jeffrey
>
> Passing a string to :conditions like this welcomes sql injection attacks
> So can it be avoided and pass an array instaed like in last post by
> fred?Am I right?
>

Only if current_contact.group is string. I assumed that it is an integer, in
which case, no SQL injection attack is possible for this call.

Jeffrey

CoolAJ86

unread,
Aug 20, 2009, 11:24:27 AM8/20/09
to Ruby on Rails: Talk
> In general that is about it. In this particular case it looks like you
> could do
>
> current_contact.group.contacts.find :all, :conditions => [["jobs.name
> LIKE ?", 'Teacher%'], ...

As I understand it, that won't eager-load (join) groups, jobs, and
contacts together.

See, as I iterate through contacts I want to pull group and job
information without excess database calls.
Reply all
Reply to author
Forward
0 new messages