QUERY BETWEEN TWO TABLES USING RUBY

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
MYSQL 
 1217  View(s)
Ratings:
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!
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Query Between Two Tables Using Ruby

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!