has_one through的目标类配置可能会导致错误
Closed this issue · 0 comments
moioo commented
class Post < ApplicationRecord
second_level_cache
belongs_to :topic, touch: true
end
class Hotspot < ApplicationRecord
belongs_to :post, required: false
has_one :topic, through: :post
end
#test
hotspot = Hotspot.create(summary: 'summary')#
assert_equal hotspot.persisted?, true
assert_nil hotspot.topic
#error
NoMethodError: undefined method `klass' for nil:NilClass
active_record/has_one_association.rb:14:in `find_target'
如果Post的topic的配置require: true,而在Hotspot中post的配置为require: false时,如果创建Hotspot时post为空 has_one就会报错