Rails正在从控制台自动加载关联

时间:2018-12-03 16:13:08

标签: ruby-on-rails associations

我有2个型号:

class User < ActiveRecord::Base
    has_one :employee, dependent: :destroy
end

class Employee < ActiveRecord::Base
    belongs_to :user
end

我注意到从控制台调用User.last时,我得到以下信息:

> User.last
  User Load (3.9ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" DESC LIMIT 1
  User Load (3.9ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" DESC LIMIT 1
  Employee Load (9.3ms)  SELECT  "employees".* FROM "employees" WHERE "employees"."user_id" = $1 LIMIT 1  [["user_id", 21198]]
  Employee Load (9.3ms)  SELECT  "employees".* FROM "employees" WHERE "employees"."user_id" = $1 LIMIT 1  [["user_id", 21198]]
#<User:0x0055bd0ae9af30> { ... }

为什么要加载员工记录?

0 个答案:

没有答案