From f4d6a04f4f40661f3b5da793ceb58d24dc56c998 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 16 Aug 2010 11:46:36 -0700 Subject: [PATCH] Get objects by bson id --- app/models/user.rb | 12 ++++++++++-- db/seeds/tom.rb | 8 -------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index f22290e18..647fe3172 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -190,12 +190,13 @@ class User end 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 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 + protected def assign_key @@ -210,4 +211,11 @@ class User OpenSSL::PKey::RSA::generate 1024 end + def ensure_bson id + if id.class == String + BSON::ObjectID(id) + else + id + end + end end diff --git a/db/seeds/tom.rb b/db/seeds/tom.rb index f171eb0f9..6f3e84582 100644 --- a/db/seeds/tom.rb +++ b/db/seeds/tom.rb @@ -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' # Create seed user