我有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> { ... }
为什么要加载员工记录?