Mongoid Without Rails

question

I‘m playing with a standalone ruby application and can‘t configure Mongoid 3.0.13 to work.

I‘ve run across a couple example apps that have configuration blocks like:

Mongoid::Config.instance.from_hash({"database" => "oid"})

Or

Mongoid.configure do |config|
 name = "mongoid_test_db"
 host = "localhost"
 port = 27017
 config.database = Mongo::Connection.new.db(name)
end

These result in:

undefined method `database=‘ for Mongoid::Config:Module (NoMethodError)

It seems the configuration settings have changed recently.

I also tried:

Mongoid::Config.connect_to("sweet")

But that seems to do nothing.

answer1

By "standalone" I‘m assuming you mean not rails. Mongoid actually provides and easy to make this work regardless of what you‘re running it in.

  1. Define a mongoid.yml file with your database connection info in it like normal.
  2. Make sure you‘ve required Mongoid in your application.
  3. Call Mongoid.load! to have Mongoid parse your configuration file and initialize itself.

    require ‘mongoid‘
    Mongoid.load!(‘/path/to/your/mongoid.yml‘)

This info can also be found here under the "Sinatra, Padrino, and others" section:
http://mongoid.org/en/mongoid/docs/installation.html

The same approach is applicable for non-webapps. Hope that helps.

answer2

The previous answer is correct to use Mongoid.load! if you want to load from a mongoid config file. I ran into a case where I needed to embed the Mongoid config in another config file. Therefore, I needed a way to load the configuration from a hash.

In >3.1, you will be able to call Mongoid.load_configuration(hash).

Unfortunately, this function is private in 3.0. Therefore, setting up a public alias method before loading Mongoid works:

module Mongoid
  module Config
    def load_configuration_hash(settings)
      load_configuration(settings)
    end
  end
end

Make sure this code gets called before require ‘mongoid‘. Now you can call Mongoid.load_configuration_hash(hash).

地址: http://stackoverflow.com/questions/15283984/mongoid-without-rails

Mongoid Without Rails

时间: 2024-11-09 03:43:32

Mongoid Without Rails的相关文章

Rails accepts_nested_attributes_for表单嵌套的利器

http://blog.csdn.net/kunshan_shenbin/article/details/7249713 http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accept

rails查询mongodb通用查询

ruby on rails 很好的跟mongodb进行了结合,gem包: 地址:https://rubygems.org/gems/mongoid 文档:https://docs.mongodb.com/ecosystem/tutorial/ruby-mongoid-tutorial/#ruby-mongoid-tutorial 最新还用的查询 Model.where(name:/abc/) 这个可以查找model表中name字段有abc的所有内容. 分页,可以结合willpage def se

RailsCast26 Hackers Love Mass Assignment rails中按params创建、更新model时存在的安全隐患

Mass assignment是rails中常用的将表单数据存储起来的一种方式.不幸的是,它的简洁性成了黑客攻击的目标.下面将解释为什么及如何解决. 上述表单为一个简单的注册表单.当用户填入name,点击提交时,一个新用户被创建.用户模型被如下定义: ruby create_table :users do |t| t.string :name t.boolean :admin, :default => false, :null => false end 当用户点击提交时,如下的action被执

Rails 5 开发进阶

Rails 5 开发进阶:https://www.gitbook.com/book/kelby/rails-beginner-s-guide/details cancan : http://blog.xdite.net/posts/2012/07/30/cancan-rule-engine-authorization-based-library-1/ Ruby官方文档翻译(Ruby官方文档中文版) : http://blog.csdn.net/liuk10/article/details/509

nginx unicorn 来运行rails

一.安装nginx sudo apt-get install nginx 安装完成后查看一下:nginx -v 说明安装成功. ubuntu系统里的安装目录是在/etc/nginx/下,启动程序文件在/usr/sbin/nginx 二.新建项目 rails new app --skip-bundle 完成后修改Gemfile文件:vim Gemfile 把source 修改成taobao或者ruby-china的源. 在这个文件里加入:gem 'unicorn' 然后运行:bundle inst

rails中是如何实现定时任务的之sidetip

环境:ubuntu14.4,ruby2.1.5, rails4.2 sidetip 依赖于sidekiq,所以在Gemfile里要添加sidekiq. 一.新一个rais项目:rails new schedule_job --skip-bundle 进入项目文件夹: cd schedule_job 打开Gemfile文件:vim Gemfile 把source 'https://rubygems.org'改为source 'https://ruby.taobao.org'. 在文件里加入:gem

rails.vim环境安装(ubuntu)

学习和开发Ruby & Rails已经一年多了.但是正式全职Rails项目的开发只有可怜的4个月经验.这期间我一直使用Netbeans 6.x作为开发工具,到目前为止我依然认为它是开发Ruby & Rails程序的最佳开发工具(我没用过最新出来的RubyMine,另外这是一个付费产品,我也不会付费用它,但也不会用D版). 在网上可以看到很到开发Rails的同学使用Vim这个神器来开发的(就在JaveEye也有不少).但是由于我的vim水平只局限于只会使用有限的几个命令( i, :w, :x

how to change the ruby and rails version

changing ruby version checking ruby version run(if you had have install rvm to manage rubies: $rvm list $rvm use ruby-2.0.0-p643 --defualt changing rails version: changing the rails versions when running rails new to build a new app(installing rails

ruby on rails如何安装

很多朋友在学习ruby语言时,因为才接触,对于ruby基础知识都不知道,比如ruby on rails安装等基础教程,下面就一起来看看安装ruby on rails教程(http://www.maiziedu.com/course/ruby/)吧: 第一,安装ruby,首先从 http://www.ruby-lang.org/zh_CN/downloads/ 下载 Ruby 的Windows 稳定安装版 Ruby 1.8.6 一步安装 (md5: 00540689d1039964bc8d844b