日期:2014-05-16  浏览次数:20615 次

Rails3.1 使用mongodb学习笔记之mongo_maper
根据官网的提示按照下面步骤依次进行,官网上的介绍文章用的是mongo_mapper但是我在网上查资料的时候发现很多人都是推荐mongoid的,本着学习,多学一点没坏处的想法,就硬着头皮弄了一遭,过几天再试一试mongoid:
安装rails
gem install rails

配置应用程序
很重要的一步就是要跳过这个active-record
rails new project_name --skip-active-record

如果是没有进行上一步而直接创建了rails项目,可以通过修改 config/application.rb 文件

#  require "rails/all" # 删除掉

# 添加下面
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"


同时要确保generate命令,不要去对对象关联active_record的orm

# Configure generators values. Many other options are available, be sure to check the documentation.
# config.generators do |g|
#   g.orm             :active_record
#   g.template_engine :erb
#   g.test_framework  :test_unit, :fixture => true
# end



安装gem初始化数据库

Gemfile中添加
source 'http://gemcutter.org'
require 'rubygems'
require 'mongo'
gem "rails", "3.1.0"
gem "mongo_mapper"

接下来运行指令安装gem
bundle install

如果提示没有bundle那么请先安装
gem install bundle


数据库:
创建文件config/initializers/mongo.rb添加内容:

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "project_name_#{Rails.env}"

if defined?(PhusionPassenger)
   PhusionPassenger.on_event(:starting_worker_process) do |forked|
     MongoMapper.connection.connect if forked
   end
end


测试配置

创建文件 lib/tasks/mongo.rake 添加内容:

namespace :db do
  namespace :test do
    task :prepare do
      # Stub out for MongoDB
    end
  end
end


--------------------------------
中间遇到的问题以及解决过程

问题一、运行rails s ,出现错误提示:
**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance.
  You can install the extension as follows:
  gem install bson_ext

  If you continue to receive this message after installing, make sure that the
  bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.

gems/ruby-1.9.2-p180/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
	from /home/chinacheng/.rvm/gems/ruby-1.9.2-p180/gems/execjs-1.3.0/lib/execjs.rb:5:in `<module:ExecJS>'
	from /home/chinacheng/.rvm/gems/ruby-1.9.2-p180/gems/execjs-1.3.0/lib/execjs.rb:4:in `<top (required)>'


提示缺少gem包
 gem install bson_ext

安装之后,再运行,仍然出现上面这个错误
在Gemfile中添加
gem "bson_ext"

执行rails s
**Notice: C extension not loaded 这个问题就不存在了

还剩下JavaScript runtime这个错误,查了一下原因是:
Windows下默认有Javascript引擎,所以window下不会有这个错误。Linux下才有这个错误,一般只要第一个项目安装即可,以后的项目不用重复安装。
而且这个错误是由development环境中的assets包引起的,注释掉
# Gems used only for assets and not required
# in production environments by default.
#group :assets do
#  gem 'sass-rails', "  ~> 3.1.0"
#  gem 'coffee-rails', "~> 3.1.0"
#  gem 'uglifier'
#end

就不用安装’execjs’和’therubyracer’了。
当然这不是一个好办法
如果不注释,需要在Gemfile中添加
gem 'therubyracer'
gem 'execjs'

或者是安装一个Node.js

问题二:配置文件中hash的问题
ps:关于hash,因为工作原因,我本地既有ruby1.8又有ruby1.9
这个config/initializers/wrap_parameters.rb文件,如果是在ruby1.8.7中会出现如下错误:
 syntax error, unexpected ':', expecting kEND (SyntaxError)