html
php
iphone
css
linux
xcode
ruby-on-rails
multithreading
eclipse
flash
json
algorithm
cocoa
tsql
apache
mvc
php5
api
jsp
postgresql
Model.collection retrieves the internal (ruby driver) collection instead of using mongoid. Batch insert needs to be done that way because it is not supported by mongoid itself.
Essentially, by accessing the collection directly, you are bypassing mongoid and so the fact that you declare key :u_id doesn't (and shouldn't) have any effect.
key :u_id
The solution is to just use _id instead of u_id so that the driver doesn't try to generate one for you.
_id
friends.each do |friend| friend[:_id] = friend["id"] friend.delete "id" end
However, this may have implications elsewhere in your code.
Another solution, if you are lazy, is to just not care about the extra object_id and let the driver create one for you.