我有两个模型叫做
Contract
Appendix
. 后者有范围
persisted
用于排除非持久化对象。我想为范围编写规范,但似乎我根本无法访问规范中的非持久关联。
模型:
class Contract < ApplicationRecord
has_many :appendixes
end
class Appendix < ApplicationRecord
belongs_to :contract
scope :persisted, -> { where 'id IS NOT NULL' }
end
context 'within persisted scope' do
it 'returns only persisted appendixes' do
contract = Contract.create(attributes_for(:contract))
Appendix.create(attributes_for(:appendix, contract: contract))
contract.appendixes.new
byebug
end
end
示例:
contract.appendixes
返回与相同的
contract.appendixes.persisted
尽管在byebug之前,新的非持久化附录已经初始化,这应该会有所不同(?):
(byebug) contract.appendixes
#<ActiveRecord::Associations::CollectionProxy []>
(byebug) contract.appendixes.persisted
#<ActiveRecord::AssociationRelation []>
2.5.1 :061 > c = Contract.last
2.5.1 :062 > c.appendixes
=> #<ActiveRecord::Associations::CollectionProxy []>
2.5.1 :063 > c.appendixes.new
=> #<Appendix id: nil, ...
2.5.1 :064 > c.appendixes
=> #<ActiveRecord::Associations::CollectionProxy [#<Appendix id: nil, ...
2.5.1 :065 > c.appendixes.persisted
Appendix Load (1.6ms) SELECT "appendixes".* FROM "appendixes" WHERE "appendixes"."contract_id" = $1 AND (id IS NOT NULL) LIMIT $2 [["contract_id", "b3a1645b-d4b1-4f80-9c43-6ddf3f3b7aba"], ["LIMIT", 11]]
=> #<ActiveRecord::AssociationRelation []>
我用
FactoryBot
属性直接初始化对象到DB以防万一(我以前完全通过FactoryBot初始化对象,但后来我认为FactoryBot可能不会以通常的方式与DB交互,这在FactoryBot的
documentation
问题:
我如何从规范中的contract对象中读取非持久化的附录?