ruby on rails - Delete an element in a hash without deleting the model -
i want delete element hash in rails. delete
method looks promising, , works 1 unintended consequence... deletes model.
# users. @search = user.all # unset yourself. id stored in current_user.id @users.delete(current_user.id)
the resulting @users hash in fact have own id removed... deletes model. correct way unset key in ruby / rails?
the behaviour describe isn't accurate on many counts. first, user.all
returns array, not hash, assuming you're using activerecord here, premise of question flawed.
secondly, deleting item array using array#delete
not invoke delete
on item itself. isn't true. wouldn't hash either.
thirdly, also won't delete item array @ all, unless index happens match index of item in array, isn't likely. you're wildly misinterpreting experiment.
you don't delete things array passing field belonging object (like id) array#delete
. need pass array index. need figure out index of record matching current user, , pass that delete
.
however, if want remove object array 1 of properties, using reject
better:
@users.reject! { |u| u.id == current_user.id }
Comments
Post a Comment