在Mongoid中选择embeds_many和references_many(embeds_one和references_one)之间应该考虑的关键点是什么?
我目前喜欢embeds_many过references_many,但那是造成太多的嵌套embeds_many"IES,我很担心,这可能会增加不必要的开销,到数据库中.
这已被问了很多,但我仍然面临一些问题.
我收到日期并存储为日期.我需要查询大于和小于,所以我将其更改为时间,再次尝试,但我得到了奇怪的结果.
我这样做
Class.where(:event_date.gt => Time.parse(Date.today))
Run Code Online (Sandbox Code Playgroud)
而我正在创造旧纪录,20世纪40年代,60年代和其他人.我尝试通过在最后添加.utc来转换时间,仅与Date.today进行比较,但到目前为止还没有解决问题.这是Mongoid生成的选择器
selector: {:date_utc=>{"$gte"=>Sat Sep 10 21:00:00 UTC 2011}},
Run Code Online (Sandbox Code Playgroud)
我以"2011-09-11"格式收到日期并将其存储在时间字段中.尝试解析时间utc,没有运气.
任何的想法?我正在使用Mongoid 2.0.2.以后的版本似乎与我正在使用的其他扩展不兼容.
[UPDATE]
所以问题在于1970年之前的旧日期显然.我现在的问题是如何处理它们.
给定一个任意mongoid文档如何将其转换为JSON并包含任何嵌入式结构,而不是在我的to_json语句中特别包含这些结构.
例如:
#!/usr/bin/env ruby
require 'mongoid'
require 'json'
require 'pp'
class Doc
include Mongoid::Document
include Mongoid::Timestamps
field :doc_specific_info , type: String
embeds_many :persons
end
class Person
include Mongoid::Document
field :role , type: String
field :full_name , type: String
embeds_many :addresses
embedded_in :Doc
end
class Address
include Mongoid::Document
field :full_address , type: String
end
doc = Doc.new
doc.doc_specific_info = "TestReport"
p = Person.new
p.role = 'buyer'
p.full_name = 'JOHN DOE'
doc.persons << p
a = Address.new
a.full_address = '1234 nowhere ville' …Run Code Online (Sandbox Code Playgroud) 我需要将嵌入的文档转换为自己的集合,因此可以从另一个集合中引用它.
让我们假设我有一个Parent嵌入很多Childs.我在考虑这个问题:
Parent.all.each do |p|
p.childs.all.each do |c|
c.raw_attributes['parent_id'] = p.id
end
p.save! #will save parent and cascade persist all childs onto their own coll
end
Run Code Online (Sandbox Code Playgroud)
这是一个选择吗?理想情况下,我会在控制台中运行它,我只会将mongoid映射更改embed_*为has_*,所以我不需要更改我的其余代码或使用另一个集合作为暂存.
一切都有效,除非我尝试进行大规模任务时得到:
"undefined method `attr_accesible' for Friend:Class"
型号,friend.rb:
class Friend
include Mongoid::Document
field :first_name, :type => String
field :last_name, :type => String
field :adjective, :type => String
attr_accessible :first_name, :last_name, :adjective
end
development:
sessions:
default:
database: first_development
hosts:
- localhost:27017
options:
options:
test:
sessions:
default:
database: first_test
hosts:
- localhost:27017
options:
consistency: :strong
max_retries: 1
retry_interval: 0
Run Code Online (Sandbox Code Playgroud)
思考?
当我尝试对数据库执行查询时,我有一堆正在运行的sidekiq作业失败并出现以下错误:
No server is available matching preference: #<Mongo::ServerSelector::Primary:0x70249928071560 tag_sets=[] server_selection_timeout=30 local_threshold=0.015>
Run Code Online (Sandbox Code Playgroud)
这似乎是随机发生的,而不是所有的时间.我的mongoid.yml结构如下:
hosts:
- XX.XXX.XXX.X:27000
- XX.XXX.XXX.XXX:27000
database: acbde__production
options:
connect_timeout: 20
read:
mode: :secondary
max_pool_size: 800
replica_set: acbdeReplset
Run Code Online (Sandbox Code Playgroud)
以前有人有过这个问题吗?
在config/routes.rb中:
resources :posts do
resources :comments
end
resources :pictures do
resources :comments
end
Run Code Online (Sandbox Code Playgroud)
我想允许更多的事情被评论.
我目前正在使用mongoid(mongomapper并不像我想的那样与Rails 3兼容),而注释是一个嵌入式资源(mongoid还不能处理多态关系资源),这意味着我确实需要父资源为了找到评论.
有没有优雅的方法来处理以下一些问题:
在我的控制器中,我需要在找到评论之前找到父母:
if params[:post_id]
parent = Post.find(params[:post_id]
else if params[:picture_id]
parent = Picture.find(params[:picture_id]
end
Run Code Online (Sandbox Code Playgroud)
如果我开始添加更多可评论的内容,那将会变得混乱.
也url_for([comment.parent, comment])不起作用,所以我将不得不在我的Comment模型中定义一些东西,但我认为我还需要在Comment模型中定义索引路径以及可能的编辑和新路由定义.
随着我的进一步发展,我可能会遇到更多问题.
我无法想象我是第一个尝试解决这个问题的人,是否有任何解决方案可以使其更易于管理?
如何将任何类的对象限制为一个.我的班级看起来像:
class Speaker
include Mongoid::Document
field :name, :type => String
end
Run Code Online (Sandbox Code Playgroud)
我只是想让一个扬声器的实例.一种方法是添加验证,该验证将检查已经存在的Speaker类的对象的数量.有没有红宝石的做事方式?
在文档中它说你可以使用inverse_of:nil但是并没有真正描述用例:http: //mongoid.org/en/mongoid/docs/relations.html#has_and_belongs_to_many
我假设它在一个对象有很多另一个对象的情况下很有用,所以你可以完全用inverse_of nil跳过那一边并节省一些存储空间吗?
例如:
class Post
has_and_belongs_to_many :tags
end
class Tag
has_and_belongs_to_many :posts, inverse_of: nil
end
Run Code Online (Sandbox Code Playgroud)
标签可能属于数百或数千个帖子,但帖子可能只有5个标签左右.
那么这是一个很好的用例吗?我想你仍然可以做到
tag.posts
Run Code Online (Sandbox Code Playgroud)
像正常一样,主要的权衡是它改变了以下的查询:
Post.find(tag.post_ids)
Run Code Online (Sandbox Code Playgroud)
成
Post.where(tag_ids: tag.id)
Run Code Online (Sandbox Code Playgroud)
如果你有一个tag_ids的索引,它似乎仍然会很快.所以也许最好的是:
class Post
has_and_belongs_to_many :tags, index: true
end
class Tag
has_and_belongs_to_many :posts, inverse_of: nil
end
Run Code Online (Sandbox Code Playgroud)
只是想检查一下我的想法.
我将在一个应用程序中利用Mongoid的单一集合继承.但是,有一个地方我想禁用此功能.我正在考虑数据库迁移(使用mongoid_rails_migrations gem),我重新定义模型以使我的迁移更易于维护.在这种情况下,我希望将该_type字段视为普通属性.
如何在Mongoid中实现它?