Re: [Rails] Searching for area code and phone number together
hi,
Make a two model, Lawyer and Phone then in lawyer model add has_any :phones and in phone model add belongs_to :lawyer
then find lawler like
@lawyer = Lawyer.find(1) 1 is id in lawyer table
then write
@lawyer.phones (it will fetch all the phone number of this lawyer havind id =1)
you can add where clause
@lawyer.phones.where(:area_code => area_code, :number =>
number)On Tue, Apr 24, 2012 at 7:26 AM, Rafael C. de Almeida <almeidaraf@gmail.com> wrote:
Hello
I have two tables: Lawyer and Phone. Phone is separated into area code
and number. A lawyer has many phones. I want to produce a query which
searches for lawyers who have a phone matching a phone from a list of
phones.
If I had only one phone I could search it like this:
Lawyer.join(:phones).where(:area_code => area_code, :number =>
number)
The problem is that I have a list with more than one area code. So I
really want to do something like this:
lawyers = []
phones.each { |phone| lawyers +=
Lawyer.join(:phones).where(:area_code => phone[:area_code], :number =>
phone[:number]) }
However, I don't want to make many queries. This is how I would do a
similar thing using SQL alone (assuming the list of numbers was
[{:area_code=>'555', :number=>'1234564'},
{:area_code=>'533', :number=>'12345678'}])
select * from phones where (area_code, number) in (('555',
'1234564'), ('533', '12345678'))
How to translate that to ActiveRecord?
Cheers,
Rafael
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home