Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 铁路协会_Ruby On Rails - Fatal编程技术网

Ruby on rails 铁路协会

Ruby on rails 铁路协会,ruby-on-rails,Ruby On Rails,我有联系人表格和公司id也可以是公司 因此,我希望有Contact.first.company和Contact.first.employees 我在我的联系人模型中创建了这样的关联 has_many :employees, :class_name => "Contact", :foreign_key => 'company_id' belongs_to :company, :class_name => "Contact" 但是Contact.first.employe

我有
联系人
表格和
公司id
也可以是
公司

因此,我希望有
Contact.first.company
Contact.first.employees

我在我的
联系人
模型中创建了这样的关联

  has_many :employees, :class_name => "Contact", :foreign_key => 'company_id'
  belongs_to :company, :class_name => "Contact"
但是
Contact.first.employees
返回空数组而不是我的记录

我有

company = Conctact.first # => id=1
employee = Contact.last.company_id # => 1
我哪里错了?

做一件事

接触模型

belongs_to :company
has_many :employees, through: :company
公司内模式

has_many :contacts
has_many :employees
现在


将给出一个结果

为什么联系模式有很多:员工,公司应该有employees@GouravNaik因为联系人可以是company(is_company:true)“return me empty array”-那么,你有company_id=1的记录吗?@SergioTulentsev是的,我有
联系人。最后。contact_id
-这个?这是错误的字段。我不想创建添加模型it@xxx你说一个
联系人
可以是一个
公司
,那么这是否意味着
联系人
也可以是其他东西?如果是这样的话,您可能正在查看多态关联的场景
Contact.first.company
Contact.first.employees