我有下一个型号:
class User < ActiveRecord::Base
end
class Customer < User
has_many :orders, foreign_key: 'customer_id', dependent: :destroy
end
class Translator < User
has_many :orders, foreign_key: 'translator_id'
end
class Order < ActiveRecord::Base
belongs_to :customer, class_name: "Customer"
belongs_to :translator , class_name: "Translator"
end
当我尝试使用翻译和客户加载订单时,我遇到了一个问题:
Order.eager_load(:vox_logs,:customer, :translator)
在这种情况下,客户将被正常加载但翻译不是:
order.translator = nil
order.translator_id = 123
我该如何解决?
编辑: 我添加了控制台输出更清晰(它来自订单的功能):
(byebug) translator
nil
(byebug) translator_id
637
(byebug) Translator.find translator_id
Translator Load (70.1ms) SELECT "users".* FROM "users" WHERE ((users.roles_mask & 4) > 0) AND "users"."id" = $1 LIMIT 1 [["id", 637]]
<Translator id: 637, email: nil, first_name: nil, middle_name: nil,
last_name: nil, native_language: "fr", country: "RU", city: "Липецк"...