jeudi 27 août 2015

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

I have a piece of code that looks something like:

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

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

If foreign_object is nil, It will only find Documents with foreign_id = NULL. Is there a way to conditionally put the foreign_id in? I essentially want to write:

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

Aucun commentaire:

Enregistrer un commentaire