On 24 April 2012 20:32, Rafael C. de Almeida <[email protected]> wrote:

> On Apr 24, 3:24 pm, Rogerio Medeiros <[email protected]> wrote:
> >  phones = [{area_code:'31', number:'32210412'},
> > {area_code:'32', number:'32210412'}]
> >
> > lawyers = []
> > phones.each do |phone|
> >   lawyer << Lawyer.joins(:phones).where('phones.area_code = ? and
> > phones.number ', phone.area_code, phone.number)
> > end
>
> Yes, that was the best approach I found, but it's still not the best,
> since it makes several SQL queries instead of only the one needed. I
> guess active record just isn't flexible enough to create a query such
> as select * from phones where (area_code, number) in (('555',
> '1234564'), ('533', '12345678')). Maybe that is not supported by all
> databases that activerecord supports? I think that approach is
> probably the best way to go for now. I was just hoping someone would
> come up with something which looks like .where([:area_code, :number]
> => [['31','322210412'], ...]).
>
> I have a lot of experience with SQL, but not much with activerecord.
> That's why the activerecord solution strikes me as a bit odd. The
> runtime of the multiple queries against the single one probably won't
> be too different if I have (area_code, number) index.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> I insist on includes():

Lawyer.includes(:phones).where("(phones.area_code in (:area_codes) and
phones.number = :phone_no)", :area_codes => [31, 32], :phone_no => '
322210412 ')

Or, if  phone nos. are different:

phones = [['31', '32221'0412'], ['32','422020202']].map{|q|
"phones.area_code = #{q[0]} and phones.number = #{q[1]}"}.join(' or ')
Lawyer.includes(:phones).where(phones)

Regards.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to