sql - Rails ActiveRecord: Find from ActiveRecord result in O(1) time -
simple question here. have active record results this:
@users = user.all
later on, want data on user ispecific id. user.find('c5ab1bfc-90ac-4b59-b5d3-fd8940aab7b1')
make query database. know user in list @users, there way find user @users id, without making db query, , without looping on @users object?
yep can use #detect select first match in relation without firing off query:
@user = @users.detect{ |u| u.id == 'c5ab1bfc-90ac-4b59-b5d3-fd8940aab7b1' }
but if running simple #find query on user (user.find params[:id]), that's not going kill application, unless i'm terribly mistaken.
updated:
thanks d34n5 correcting me, #select iterate through entire collection whereas #detect (aliased #find) stop @ , return first occurrence.
Comments
Post a Comment