ruby on rails - How to raise ActiveRecord::RecordNotFound if not all records are found? -
given item id of 1 exists in database, item id of 2 not, following line:
item.find([1, 2])
will raise activerecord::recordnotfound
error. however, following line not:
item.where(id: [1, 2])
instead, return item id of 1. there way enforce behavior of find
on where
if querying on unique values.
i have model foo
, which, apart id
, has uid
string attribute. each foo
has unique uid
value. given array of uids
, i'd able either fetch foos
or raise error, if 1 of uid
values foo
doesn't exist.
p.s. raise activerecord::recordnotfound unless query_result.count == uids.count
, want know, if there better approach.
i think there isnt such provision where
(which returns relation instead of actual ar objects).
i checked source code , looks activerecord
uses same trick raise recordnotfound
exception find when cant find id array passed. (match size of results against size of ids array passed) referred method find
calls here , place raise exception here
Comments
Post a Comment