[英]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. 我有两个模型contacts
和signups
,也有相同的控制器。
What I want here is to get all data from contacts table where contacts table id = signups table contact_id. 我想要的是从联系人表获取所有数据,其中联系人表ID =注册表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. 但我只想获取注册表中存在其ID的数据。
you are not following rails conventions 您没有遵循Rails约定
Usercontacts
should be UserContact
with corresponding table name user_contacts
(or Contact
if you already have contacts
table) Usercontacts
应该是具有相应表名user_contacts
UserContact
(如果已有contacts
表,则为Contact
)
Signups
should be Signup
with corresponding table name signups
Signups
应该是Signup
有相应的表名signups
relation declarations follow same conventions - belongs_to :singular_name
, has_many :plural_name
关系声明遵循相同的约定has_many :plural_name
belongs_to :singular_name
, has_many :plural_name
if you start following conventions all your problems will go away 如果您开始遵循约定,所有问题都会消失
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.