如何在 ActiveRecord 的 where 中有条件地调用属性?

How can I conditionally call attributes within ActiveRecord's where?

我有一段代码看起来像:

foreign_object = (some_test) ? nil : <some foreign_object>

Document.where(foreign_id: foreign_object, status: "Working", ... )

如果foreign_object为nil,它只会找到foreign_id = NULL的文档。有没有办法有条件地将 foreign_id 放入?我基本上想写:

Document.where(foreign_id: foreign_object if foreign_object, status: "Working", ...)

遇到这种情况我会这样做:

 documents = Document.where(status: 'working')
 documents = documents.where(foreign_id: foreign_object) if foreign_object