0
我有两个表被以下如何从两个数据库表的数据在Ruby on Rails的
contacts table
id
email
name
company
phone
和
signups table
id
contact_id
code
details
我有两个型号contacts
和signups
,并具有相同的控制器作为好。
我想要的是从联系人表中获取所有数据,其中联系人表id =注册表contact_id。
我该如何在导轨上做红宝石?
更新
这里是我的模型,其是空的,现在
class Usercontacts < ActiveRecord::Base
#has_one :signups
#has_one :receiver, :class_name => "Signups"
end
这里是第二个模型
class Signups < ActiveRecord::Base
attr_accessible :contact_id, :code, :event_id, :details
#belongs_to :usercontacts
#belongs_to :receiver, :class_name => "Usercontacts"
end
现在我做这样的事情我的申请人数控制器
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
但这从联系人表中获取所有数据。但我只想获取注册表中存在其ID的数据。
http://guides.rubyonrails.org/association_basics.html,将是一个很好的起点。 – 2012-07-17 07:54:29
而且这也是:http://guides.rubyonrails.org/active_record_querying.html#joining-tables – Mischa 2012-07-17 08:04:43
你的模特是什么样的?你有什么尝试?你必须表现出一些努力,不要期望人们为你写代码。 – Mischa 2012-07-17 08:06:58