Get objects by bson id

This commit is contained in:
Raphael 2010-08-16 11:46:36 -07:00
parent 2c1a67eb0f
commit f4d6a04f4f
2 changed files with 10 additions and 10 deletions

View file

@ -190,12 +190,13 @@ class User
end end
def friend_by_id( id ) def friend_by_id( id )
friends.detect{|x| x.id == id || x.id == BSON::ObjectID(id) } friends.detect{|x| x.id == ensure_bson id }
end end
def group_by_id( id ) def group_by_id( id )
groups.detect{|x| x.id == id || x.id == BSON::ObjectID(id) } groups.detect{|x| x.id == ensure_bson id }
end end
protected protected
def assign_key def assign_key
@ -210,4 +211,11 @@ class User
OpenSSL::PKey::RSA::generate 1024 OpenSSL::PKey::RSA::generate 1024
end end
def ensure_bson id
if id.class == String
BSON::ObjectID(id)
else
id
end
end
end end

View file

@ -1,11 +1,3 @@
#This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
# Mayor.create(:name => 'Daley', :city => citie
require 'config/environment' require 'config/environment'
# Create seed user # Create seed user