我正在开始一个新的应用程序,并注意到我上次从头开始构建MongoID应用程序时缺少一些文档.也就是说,他们曾经在不再存在的页面上建议(http://mongoid.org/docs/integration/)包含一些代码来删除MongoID的集合(在测试之后).
它现在不再被提及了... 这是(下面的****)不再被视为必要或良好的做法?!?
#spec/spec_helper.rb:
...
RSpec.configure do |config|
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
#config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
#config.use_transactional_fixtures = true
# Below from <http://mongoid.org/docs/integration/> ****
config.after :suite do
Mongoid.master.collections.select do |collection|
collection.name !~ /system/
end.each(&:drop)
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个食谱模型,其中嵌入了成分,使用Mongoid.
class Recipe
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
embeds_many :ingredients
accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:title].blank? }, :allow_destroy => true
validates :title, :presence => true
end
class Ingredient
include Mongoid::Document
field :name, :type => String
field :quantity, :type => String
embedded_in :recipe, :inverse_of => :ingredients
end
Run Code Online (Sandbox Code Playgroud)
我希望能够同时创建一个新的配方,以及该配方的相关成分,但我很难理解我将如何做到这一点.这是我到目前为止:
_form.html.erb - 在配方视图中使用
<%= form_for @recipe do |f| %>
...
<li>Title: <%= f.text_field :title %></li>
<% f.fields_for :ingredients do |builder| %>
<%= render "ingredient_fields", :f => builder …Run Code Online (Sandbox Code Playgroud) ruby-on-rails mongodb mongoid ruby-on-rails-3 ruby-on-rails-3.1
有了这些型号:
class Week
has_many :proofs
end
class Proof
belongs_to :week
end
Run Code Online (Sandbox Code Playgroud)
我想做的事情如下:
Week.where(:proof.count.gt => 0)
Run Code Online (Sandbox Code Playgroud)
仅查找具有多个证明的周数.
有一个答案似乎可以解决这个问题:
但是在这个例子中,由于ids与证明一起存储,因此在周中没有诸如proof_ids之类的属性.这不适用于例如:
Week.where(:proof_ids.gt => 0)
Run Code Online (Sandbox Code Playgroud)
这个查询怎么可能?概念上很简单,但我无法弄清楚如何用mongo或mongoid做到这一点.
同样,我想按照证明的数量排序,例如:
Week.desc(:proofs.size)
Run Code Online (Sandbox Code Playgroud)
但这也行不通.
我确实意识到反缓存是我的两个具体问题的选项,但我也希望能够进行查询.
在此先感谢您的帮助.
在一个简单的mongoid数据模型上,用户有很多评论,我想在用户写至少一条评论时给用户一个特定的徽章.所以我建立了一个像这样的观察者:
class CommentBadgeObserver < Mongoid::Observer
observe :comment
def after_create(comment)
CommentBadge.check_conditions_for(comment.user)
end
end
class CommentBadge < Badge
def self.check_conditions_for(user)
if user.comments.size > 1
badge = CommentBadge.create(:title => "Comment badge")
user.award(badge)
end
end
end
Run Code Online (Sandbox Code Playgroud)
user.award方法:
def award(badge)
self.badges << badge
self.save
end
Run Code Online (Sandbox Code Playgroud)
以下测试失败(但我想这是正常的,因为观察者在后台执行?)
it 'should award the user with comment badge' do
@comment = Factory(:comment, :user => @user)
@user.badges.count.should == 1
@user.badges[0].title.should == "Comment badge"
end
Run Code Online (Sandbox Code Playgroud)
什么是验证此行为的最佳方法?
我有两个问题,
Post.where(:group_id.in => group_ids, :deleted => false)
Post.where(:user_id=>user.id,:deleted=>false)
我需要使用或条件组合这些查询.我试过,
Post.where(:deleted => false).or({:user_id=>user.id},{:group_id.in => group_ids})
和
Post.any_of({:group_id.in=>group_ids},{:user_id=>user.id})
,但我没有得到结果.
我读到mongoid中的继承,似乎所有继承的类都将保存在基类中,例如
class BaseClass
end
class ChildClass1 < BaseClass
end
class ChildClass2 < BaseClass
end
Run Code Online (Sandbox Code Playgroud)
似乎所有这些都存储在BaseClass集合中.
我实际上希望它们存储在单独的集合中,例如ChildClass1 - collection和ChildClass2 - collection.
我一直在摆弄Mongo,但无法让这个简单的例子起作用.我只是想要检索集合中的所有文档:
require 'mongoid'
# configuration ...
class Category
include Mongoid::Document
field :name, type: String
end
Category.each do |test|
puts test.inspect
end
Run Code Online (Sandbox Code Playgroud)
我收到错误: undefined method 'each' for Category:Class (NoMethodError).
已建立与数据库的连接,名为的集合categories包含一些文档.
所以我正在玩mongo的东西.创建了一个新的heroku应用程序,为它添加了一个mongolab选项,但每个mongoid方法都失败了.
我用Google搜索了,看起来这个问题在1.9.3之前的ruby很常见,但我运行的是1.9.3.
这是最简单的失败样本:
require 'sinatra'
require 'mongoid'
require 'json'
require "sinatra/reloader" if development?
Mongoid.load!("mongoid.yml")
class Company
include Mongoid::Document
field :code, type: String
field :sector, type: String
field :share_id, type: Integer
field :jse_code, type: Integer
end
get '/' do
"Hello"
end
get '/company' do
Company.exists?
end
Run Code Online (Sandbox Code Playgroud)
我在本地运行(对mongolab db)或在heroku上运行,我得到同样的错误:
12:22:33 web.1 | NoMethodError - undefined method `match' for nil:NilClass:
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions/mongo_uri.rb:49:in `initialize'
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:103:in `new'
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:103:in `parse'
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:62:in `create_session'
12:22:33 web.1 | …Run Code Online (Sandbox Code Playgroud) 我找不到任何博客文章或文档谈论这个.它们,嵌入式文档和哈希数据类型都非常相似.每个人的利益或限制是什么?
考虑我的架构设计:
class HistoryTracker
include ::Mongoid::Document
include ::Mongoid::Timestamps
field :modifier, type: Hash, default: {}
field :original, type: Hash, default: {}
field :modified, type: Hash, default: {}
field :changeset, type: Hash, default: {}
end
Run Code Online (Sandbox Code Playgroud)
我应该在这个HistoryTracker类中创建几个嵌入式文档吗?或者只是用它?索引怎么样?
当在我的Rails应用程序某型号查询,它返回正确的结果,摘取size,length或count信息,即使使用limit标准.
recipes = Recipe
.where(:bitly_url => /some.url/)
.order_by(:date => :asc)
.skip(10)
.limit(100)
recipes.size # => 57179
recipes.count # => 57179
recipes.length # => 57179
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么会这样,它会一直显示食谱集合的总数,而且自从我使用以来,正确的值应该是100 limit.
count = 0
recipes.each do |recipe|
count += 1
end
# WAT
count # => 100
Run Code Online (Sandbox Code Playgroud)
有人能帮助我吗?谢谢!
-
Rails版本:3.2.3
Mongoid版本:2.4.10
MongoDB版本:1.8.4