Advertise with us

Query Between Two Tables Using Ruby

Posted by  jayprakash sharma
 2026  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'")

contact.each do|c|
 arr << c.app_id
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. 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.