我尝试编写应用程序的业务逻辑。都是ruby类。没有像 Rails、Sinatra 这样的数据库或 UI 框架。我只有一个关于业务逻辑的 Gem_file,并且 Gem_file 只包含“mini_test”gem。我使用 mini_test 来测试业务逻辑。现在,我需要向系统添加一个数据库。我怎样才能做到这一点? mongoid 配置在 Rails 上的 application.file 中进行。但是,我不使用 Rails 或任何其他框架。无论如何,没有像 Rails,Sinatra 这样的框架来配置 mongoid。 我希望我能解释我的问题。另外,我在下面添加我的代码:
这是我的上下文-
class HeadTeacherDefineAcademicYearContext
attr_reader :person, :academicyear
def initialize(person, academicyear)
@person, @academicyear = person, academicyear
@person.extend HeadTeacher
end
def call
@person.define_academic_year @academicyear
end
end
这是我的角色模块
module HeadTeacher
def define_academic_year(academicyear)
#i write db save process here using any database
end
end
我的模型类
class AcademicYear
attr_accessor :year
end
最佳答案
您必须在 Gemfile 中包含 gem 'mongoid'
并安装它。之后,您可以在需要的地方要求和初始化 Mongoid:
require 'mongoid'
Mongoid.load!("mongoid.yml", :development)
它需要一个带有配置的 mongoid.yml
文件。示例:
development:
sessions:
default:
database: myapp_development
hosts:
- localhost:27017
当然,您可以使用不同于:development
的上下文,也许通过环境变量分配它。现在,将 Mongoid::Document
添加到您的模型中:
class AcademicYear
include Mongoid::Document
field :year, type: Integer
end
关于ruby - 没有框架的ruby上的mongoid配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15832556/