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
爲什麼不加載報價?在此先感謝