Home > DeveloperSection > Forums > Query Between Two Tables Using Ruby
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    May-18-2013 1:18 AM

 MYSQL MYSQL 
Ratings:


 1 Reply(s)
 927  View(s)
Rate this:
Hi Guys! 

How to used query between two table in rub?

I'm trying as following

contact = UserContact.find(:all,:select=>"distinct app_id,number",:conditions=>"number ='1234'")

arr=[]
contact.each do|c|
 arr << c.app_id
end
name=User.find(:all,:conditions=>"id in(#{arr.join(',')}")
I takes two much time Can i do this using join

Please help!
Thanks in advance!


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-18-2013 8:26 AM

Hi Jayprakash!

You should do something like this

User.find(:all, :joins => :user_contacts, :conditions => "user_contacts.number = '1234'")
user should have association. In user.rb should be:

has_many :user_contacts, :foreign_key => :app_id
But it is bad style to name column "app_id" it should be "user_id"(convention over configuration). Renamed it. After rename you can remove ", :foreign_key => :app_id"

I hope it helpful for you.


Don't want to miss updates? Please click the below button!

Follow MindStick