Om3ga
Om3ga

Reputation: 32853

How to get data from two database tables in ruby on rails

I have two tables which are following

contacts table
id
email
name
company
phone

and

signups table
id
contact_id
code
details

And I have two models contacts and signups and have same controllers as well.

What I want here is to get all data from contacts table where contacts table id = signups table contact_id.

How can I do this in ruby on rails?

Update

Here are my models which are empty for now

class Usercontacts < ActiveRecord::Base
#has_one :signups
#has_one :receiver, :class_name => "Signups"
end

HEre is second model

class Signups < ActiveRecord::Base
 attr_accessible :contact_id, :code, :event_id, :details

 #belongs_to :usercontacts
 #belongs_to :receiver, :class_name => "Usercontacts"
end

Now I am doing something like this my signups controller

class SignupsController < ApplicationController
layout 'admin_layout'

def signups
    #@signups = Contact.joins('LEFT OUTER JOIN signups ON contacts.id = signups.contact_id')
            @contacts = Contact.joins(:sign)
end
end

but this gets all the data from contacts table. but I want to get only that data which is whose id is present in signups table.

Upvotes: 0

Views: 1645

Answers (1)

keymone
keymone

Reputation: 8094

you are not following rails conventions

Usercontacts should be UserContact with corresponding table name user_contacts(or Contact if you already have contacts table)

Signups should be Signup with corresponding table name signups

relation declarations follow same conventions - belongs_to :singular_name, has_many :plural_name

if you start following conventions all your problems will go away

Upvotes: 5

Related Questions