所以我在 mongo 中有一个带有created_at字段的集合,我试图sort_date为每个文档添加一个属性,该属性最初等于created_at但可由用户修改。我正在使用带有 Rails 的 Mongoid 并尝试编写迁移,我想知道是否有一种聪明的(阅读:有效的)方法可以将一个属性的值复制到另一个属性,而无需 mapReduce 或循环遍历 Ruby 中的模型。谢谢!
我有一个具有以下代码的 ArticleController:
def edit
@article = current_user.articles.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
以及以下测试:
describe "GET 'edit'" do
it "should fail when signed in and editing another user article" do
sign_in @user
get :edit, :id => @another_user_article.id
response.should_not be_success
end
end
Run Code Online (Sandbox Code Playgroud)
但是,当我开始测试时,出现以下错误(这是正常的),但我想知道如何处理以便我的测试通过?
Failure/Error: get :edit, :id => @another_user_article.id
Mongoid::Errors::DocumentNotFound:
Document not found for class Article with id(s) 4f9e71be4adfdcc02300001d.
Run Code Online (Sandbox Code Playgroud)
我想通过这个改变我的控制器方法,但这对我来说似乎不对:
def edit
@article = Article.first(conditions: { _id: params[:id], user_id: current_user.id })
end
Run Code Online (Sandbox Code Playgroud) 我在使用 MongoDB 和 Mongoid 的 Rails 应用程序中工作。我可以使用 mongo shell 环境运行 mongo 查询,但我很想在 irb 中使用 Mongoid。这是我能做的吗?如果是这样,有人可以告诉我怎么做吗?
谢谢 :)
我正在使用 sinatra 1.4.3 和 mongoid 3.1.4。我尝试从 master 分支添加 will_paginate gem 以获得 mongoid 支持,所以我将它添加到我的 gemfile 中:
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git',
:branch => 'master'
Run Code Online (Sandbox Code Playgroud)
在 environment.rb 中,我添加了:
require 'will_paginate'
require 'will_paginate/mongoid'
Run Code Online (Sandbox Code Playgroud)
分页方法开始工作。我对 will_paginate 助手仍有问题。在我看来,我收到如下错误:
NoMethodError: undefined method `will_paginate' for #<Class:0x006ff5df8578b0>
Run Code Online (Sandbox Code Playgroud)
我是否缺少帮助在 sinatra 下工作的东西?
我想知道是否有某种方法可以使用 Mongoid 忽略模型命名空间。我正在将所有模型移动到 Rails 引擎,并为它们命名。我已经能够在没有命名空间的情况下将它们添加到 rails 引擎并且它引用正常,但是我们正在努力转向面向服务的架构,我想命名所有模型。
这是之前和之后的示例模型
# Before
class Model
include Mongoid::Document
field :field1
end
# After
module Engine
class Model
include Mongoid::Document
field :field1
end
end
Run Code Online (Sandbox Code Playgroud)
这是我执行时控制台中发生的情况 Engine::Model.all
=> #<Mongoid::Criteria
selector: {}
options: {}
class: Engine::Model
embedded: false>
Run Code Online (Sandbox Code Playgroud)
如果我能做到让 mongoid 寻找Model它,它就会完美地处理我的数据。
理想情况下,我能够做到Engine::Model.all,它会返回这个
=> #<Mongoid::Criteria
selector: {}
options: {}
class: Model
embedded: false>
Run Code Online (Sandbox Code Playgroud)
有什么办法可以做到这一点吗?
我想为控制器的 :date 字段自动添加今天的日期。我是 Rails 的新手,不知道如何设置这个默认值,除了它必须在控制器中。
这是我的 Post 模型:
class Post
include Mongoid::Document
field :title, type: String
field :text, type: String
field :date, :type => DateTime
end
Run Code Online (Sandbox Code Playgroud)
还有我的 Post 控制器:
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
private
def post_params
params.require(:post).permit(:title, :text, :date)
end
Run Code Online (Sandbox Code Playgroud)
查看 Rails 文档,我了解了 Date.current 或 Date.today。
假设我已经用几个索引定义了我的模型 Person:
class Person
include Mongoid::Document
field :email
field :ssn
index({ email: 1 }, { unique: true })
index({ ssn: 1 }, { unique: true })
end
Run Code Online (Sandbox Code Playgroud)
但是,只有电子邮件索引已经存在于数据库中,所以当我调用
Person.collection.indexes.each {|i| puts i.inspect}
Run Code Online (Sandbox Code Playgroud)
我收到以下回复:
{"v"=>1, "key"=>{"_id"=>1}, "name"=>"_id_", "ns"=>"x.person"}
{"v"=>1, "unique"=>true, "key"=>{"email"=>1}, "name"=>"email_1", "ns"=>"x.person"}
Run Code Online (Sandbox Code Playgroud)
问题是,即使它们尚未在 mongo 中创建,我如何获取模型中已定义索引的列表?
就我而言,此类列表应包括“ssn”字段的定义
换句话说...如何获取那些尚未创建的索引?
我有一个嵌入集合B的集合A.集合A和集合B包括Mongoid时间戳(created_at和updated_at).
现在,当我使用Rails管理员创建集合B(嵌入对象)的新条目时,保存在数据库中的时间戳为零.但是如果我从rails控制台或普通api创建一个条目,那么保存在数据库中的时间戳不是零.
任何帮助,将不胜感激.
编辑:
class B
include Mongoid::Document
include Mongoid::Timestamps::Created
include Mongoid::Timestamps::Updated
field :user_id, type: String
field :message, type: String
field :status, type: Integer, default: 1
field :spam_count, type: Integer, default: 0
embedded_in :A
Run Code Online (Sandbox Code Playgroud)
B类嵌入在A类中.当在A通过rails admin创建B的条目时,B的created_at和updated_at字段将保存为nil.
因此,我需要在创建对象之前(从表单)检查三个字段的唯一性,但只要三个字段中的任何一个是唯一的,我就会创建对象。
我的第一个想法是将参数从控制器传递给模型,然后运行查询以检查具有这三个字段的查询是否返回 > 0 个文档。但是,我后来了解到这是一种危险的方法,不应使用。
所以我检查了文档,并基于这个片段
Or even multiple scope parameters. For example, making sure that a teacher can only be on the schedule once per semester for a particular class.
class TeacherSchedule < ActiveRecord::Base
validates_uniqueness_of :teacher_id, scope: [:semester_id, :class_id]
end
Run Code Online (Sandbox Code Playgroud)
我以为我找到了答案,并实施了:
validates_uniqueness_of :link_to_event, :scope => [:name_of_event, :date_of_event]
Run Code Online (Sandbox Code Playgroud)
哪个有效!但是,这个数据集会变得非常大(不仅仅是这个表单,哈哈),我的印象是,通过这个实现,Rails 将查询所有带有 link_to_event 的字段,然后所有带有name_of_event,然后是带有 date_of_event 的所有字段。所以,我的问题是:
A) Rails 将如何实现这一点我错了吗?开箱即用会更有效率吗?
B)如果这对于具有几百万个条目的表来说效率不高,是否有更好(并且仍然很笨拙)的方法来做到这一点?
在我的测试中,Moingoid似乎没有持久地设置嵌入式关系.在我的用户模型中,我有:
def vote_on(bill, value)
if my_groups = self.groups
my_groups.each do |g|
bill.votes.create(:value => value, :user_id => self.id, :group_id => g.id)
# result only with factories: bill.votes.first.group = nil
# and bill.votes.first.user = nil !!
# self.id and g.id have good values during the test, they just aren't persisting
end
else
raise "no groups for this user" # #{self.full_name}"
end
end
其他有用的代码可能是:
## bill model class Bill embeds_many :votes ## vote model class Vote include Mongoid::Document field :value, :type => Symbol …