0
我有一个模型Spree::Quotation
如下ActiveRecord的belongs_to的关联返回nil
class Spree::Quotation < ActiveRecord::Base
has_one :payment_term, -> { where(service_type: 'Spree::Quotation') },
class_name: 'PaymentTerm',
foreign_key: 'service_id'
end
这是PaymentTerm
模型
class PaymentTerm < ActiveRecord::Base
belongs_to :quotation, -> { where(service_type: 'Spree::Quotation') },
class_name: 'Spree::Quotation',
primary_key: 'service_id'
end
这是payment_terms
表的内容
development=# SELECT "payment_terms".* FROM "payment_terms";
id | service_id | service_type | term |
----+------------+------------------+--------------+
1 | 1 | Spree::Quotation | 100% upfront |
(1 row)
现在时我做Spree::Quotation.first.payment_term
我收到payment_长期正确
2.1.8 :007 > Spree::Quotation.first.payment_term
Spree::Quotation Load (1.0ms) SELECT "spree_quotations".* FROM "spree_quotations" ORDER BY "spree_quotations"."id" ASC LIMIT 1
PaymentTerm Load (0.7ms) SELECT "payment_terms".* FROM "payment_terms" WHERE "payment_terms"."service_id" = $1 AND "payment_terms"."service_type" = 'Spree::Quotation' LIMIT 1 [["service_id", 1]]
=> #<PaymentTerm id: 1, service_id: 1, service_type: "Spree::Quotation", term: "100% upfront",... >
但是当我做PaymentTerm.first.quotation
,我越来越nil
2.1.8 :008 > PaymentTerm.first.quotation
PaymentTerm Load (0.8ms) SELECT "payment_terms".* FROM "payment_terms" ORDER BY "payment_terms"."id" ASC LIMIT 1
=> nil
为什么不加载报价?在此先感谢