It can be done via ActiveRecord#where, but you lose the semantics of using 
#find. Under the hood, I suppose ActiveRecord just does a #where and then 
compares the results, so you're not technically losing any optimization 
that way either (at least for the current implementation of #find).

On the other hand, it basically forces you to reimplement the #find method 
yourself if you want the additional information. I think there's a valid 
use case here, but if status quo is the community preference, I'll 
graciously concede the point.

On Tuesday, June 21, 2016 at 10:15:04 AM UTC-4, Saiqul Haq wrote:
>
> I think we can achieve this with ActiveRecord#where method, e.g.:
>
> 3.times { Book.create! } #=> which returned 3 records, id = 1, 2, 3
>
> then I want to find Book records with ID 1, 2, and 5
> so 
> expected_records = [1,2,5]
> books = Book.where id: expected_records
>
> then to get to know which  results were missing is by 
>
> missing_records = expected_records - books.pluck(:id)
>
> Pada Selasa, 21 Juni 2016 11.34.23 UTC+7, Robert Smith menulis:
>>
>> When I perform an ActiveRecord::Base.find(...) operation with multiple 
>> ids, and there is at least one result that is not found, ActiveRecord 
>> raises a RecordNotFound error. The error message lists the requested ids 
>> and the number of results requested and found. An example:
>>
>> class Book < ActiveRecord::Base; end
>>
>> 3.times { Book.create }
>> # Creates books with ids 0, 1, 2
>>
>> begin
>>   Book.find(0, 1, 5)
>> rescue ActiveRecord::RecordNotFound => exception
>>   puts exception.message
>> end
>> # "Couldn't find all Books with 'id': (0, 1, 5) (found 2 results, but was 
>> looking for 3)"
>>
>> As a developer, I want to know which results were returned and which were 
>> not. In our example, ActiveRecord::FinderMethods#find_some actually does 
>> have the found results, which it checks against the expected size. However, 
>> the result itself is discarded and only the result size is passed into the 
>> error via the #raise_record_not_found_exception! method.
>>
>> My proposal is to add an additional, optional #result property to the 
>> RecordNotFound error, which is set to the result of the query. This allows 
>> downstream code to know which records were found (and deduce which records 
>> were not found by comparing the found records with the expected ids). For 
>> example, this could allow a controller to display a more helpful error 
>> message to the end user, or allow a library to wrap the RecordNotFound with 
>> additional logic around found/missing records.
>>
>> I've built a quick example branch, see 
>> https://github.com/sleepingkingstudios/rails/commit/e11a70ef60bbb60e57a081c6e313580880b4570a
>> .
>>
>> Any feedback would be appreciated.
>>
>> Regards,
>>
>> Rob Smith
>>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to