Rails3.2使用mongoDB学习笔记之mongoid
MongoDB  /  houtizong 发布于 3年前   233
rails new spec_mongoid
gem "mongoid" gem "bson_ext"
$ rails g mongoid:config
create config/mongoid.yml
gem 'execjs' gem 'therubyracer'
# require 'rails/all'
require "action_controller/railtie" require "action_mailer/railtie" require "active_resource/railtie" require "rails/test_unit/railtie"
# config.active_record.mass_assignment_sanitizer = :strict # config.active_record.auto_explain_threshold_in_seconds = 0.5
Mongoid.load!("path/to/your/mongoid.yml")
config.mongoid.logger = Logger.new($stdout, :warn)
logger: false
Mongoid.add_language("zh-CN")
Mongoid.add_language("*")
host is not a valid option for Mongo::Connection
gem 'mongoid', :git => 'git://github.com/mongoid/mongoid.git'
undefined method `add_language' for Mongoid:Module (NoMethodError)
#Mongoid.add_language("*")注释掉
$ rails g model user invoke mongoid create app/models/user.rb invoke test_unit create test/unit/user_test.rb create test/fixtures/users.yml
class User include Mongoid::Document end
ruby-1.9.2-p180 :003 > User.create(:name=>"李磊") => #<User _id: 4f630bf1421aa97127000001, _type: nil, name: "李磊", login: nil, age: nil>ruby-1.9.2-p180 :002 > user = User.find('4f630bf1421aa97127000001') => #<User _id: 4f630bf1421aa97127000001, _type: nil, name: "李磊", login: nil, age: nil> ruby-1.9.2-p180 :004 > user.update_attributes(:name=>"李磊",:login=>"lilei",:age=>15) => true ruby-1.9.2-p180 :005 > user => #<User _id: 4f630bf1421aa97127000001, _type: nil, name: "李磊", login: "lilei", age: 15>
# fixtures :all
# encoding: utf-8require 'test_helper'class UserTest < ActiveSupport::TestCase test "create a User" do assert_difference "User.count",1 do User.create(:name=>"李磊",:age=>15,:login=>"lilei") end lilei = User.last assert_equal [lilei.name,lilei.age,lilei.login],["李磊",15,"lilei"] end end
rake test:unitsStarted.Finished in 0.020769 seconds.1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接