【问题标题】:Rails has_many foreign key producing empty arrayRails has_many 外键产生空数组
【发布时间】:2012-06-24 07:12:59
【问题描述】:

当我在测试一些代码时,我无法弄清楚我在 rias 控制台中遇到的一些奇怪行为。

我的模型如下:

profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user
  belongs_to :company

  attr_accessible :first_name, :last_name


  validates :first_name, presence: true
  validates :last_name, presence: true

end

公司.rb

class Company < ActiveRecord::Base

  has_many :employees, :foreign_key => 'company_id', :class_name => "Profile"
  attr_accessible :name
  validates :name,  presence: true, length: { maximum: 50 }, uniqueness: true

end

我进入 rails 控制台

$rails c --sandbox

然后测试下面的代码

:001 > company = Company.find(2)
Company Load (4.2ms)  SELECT "companies".* FROM "companies" WHERE "companies"."id" = ?     LIMIT 1  [["id", 2]]
 => #<Company id: 2, name: "Jet Star", created_at: "2012-06-24 04:12:50", updated_at:  "2012-06-24 04:12:50"> 
:002 > employee = Profile.find(5)
Profile Load (0.1ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = ? LIMIT 1  [["id", 5]]
=> #<Profile id: 5, user_id: 5, first_name: "Douglas", last_name: "Reed", created_at: "2012-06-17 15:05:58", updated_at: "2012-06-17 15:05:58", deleted: false, company_id: nil> 
:003 > employee.company = company
=> #<Company id: 2, name: "Jet Star", created_at: "2012-06-24 04:12:50", updated_at: "2012-06-24 04:12:50"> 
:004 > employee.company
=> #<Company id: 2, name: "Jet Star", created_at: "2012-06-24 04:12:50", updated_at: "2012-06-24 04:12:50"> 
:005 > employee
=> #<Profile id: 5, user_id: 5, first_name: "Douglas", last_name: "Reed", created_at:  "2012-06-17 15:05:58", updated_at: "2012-06-17 15:05:58", deleted: false, company_id: 2> 
:006 > company.employees
Profile Load (0.2ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."company_id" = 2
=> [] 
:007 > Profile.find_by_company_id(2)
Profile Load (0.2ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."company_id" = 2 LIMIT 1
 => nil 
:008 > employee.save
(0.1ms)  SAVEPOINT active_record_1
(0.4ms)  UPDATE "profiles" SET "company_id" = 2, "updated_at" = '2012-06-24 07:35:14.525138' WHERE "profiles"."id" = 5
(0.0ms)  RELEASE SAVEPOINT active_record_1
=> true 
:009 > company.employees
=> []  
:010 > Profile.find_by_company_id(2)
  Profile Load (0.2ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."company_id" = 2 LIMIT 1
=> #<Profile id: 5, user_id: 5, first_name: "Douglas", last_name: "Reed", created_at: "2012-06-17 15:05:58", updated_at: "2012-06-24 07:35:14", deleted: false, company_id: 2> 

如果您查看个人资料记录,它会显示 company_id,但是当您搜索具有该 ID 的员工(个人资料)时,不会返回任何内容。

那么为什么 company.employees 不返回列表呢?

【问题讨论】:

    标签: ruby-on-rails activerecord foreign-keys has-many


    【解决方案1】:

    试试这个:

    company.reload
    company.employees
    

    【讨论】:

    • 我需要在我的控制器中执行此操作还是仅需要在控制台中执行此操作?
    • 如果所有相同的代码都在一个控制器中,那么是的,您必须 reload company 实例才能从数据库中获取正确的信息。
    【解决方案2】:

    写入“employee.company =company”只会在内存中设置员工的公司 ID。当您查询 company.employee 时,它​​会搜索员工表(在磁盘上),但由于尚未提交更改,因此在该表中找不到 id。

    【讨论】:

    • 我已经更新了问题的详细信息。如您所见,我保存了记录,但即使在我尝试访问员工列表后,数组也是空的
    • 公司中的“员工”是如何定义的?
    • 在has_many关系中,因为我说a公司有很多员工更有意义,但实际记录存储在profiles表中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 2023-03-21
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    相关资源
    最近更新 更多