Tag: mongoid

嵌入或引用的关系

我使用mongodb和mongoid gem,我想得到一些建议。 我有一个应用程序,用户has many市场和市场has many产品。 我需要在属于用户的所有(或任何)市场中搜索特定价格范围内的产品。 哪种关系更适合这种,嵌入或引用? 我目前使用引用,它看起来像这样 class User has_many :markets end class Market belongs_to :user has_many :products end class Product belongs_to :calendar belongs_to :user end 对于搜索,我使用此查询 Product.where(user_id: current_user.id). in(market_id: marked_ids). where(:price.gte => price) 我很好奇,因为mongdb是一个面向文档的数据库,如果我在这种情况下使用嵌入式文档,我会在性能或设计方面受益吗?

如何在Rail 2.3.5中生成mongoid.yml配置?

正如标题所说,我如何在Rail 2.3.5上生成默认的mongoid.yml配置文件? 我尝试使用’rails generate mongoid:config’命令,但它只是生成一个新的应用程序。 而且,我想在mongoid中使用has_many而不在相同的字段中嵌入相关的模型。 我希望它们位于不同的字段中并通过* _id“列”关联。 那可能吗?

使mongoid会话只读

我在mongoid.yml有不同的会话,其中一个会话提供来自静态mongo数据库的数据。 我想知道是否有可能在只读模式下“加载”会话,因此不能对save , create , destroy或destroy_all进行任何更改。 我的mongoid.yml看起来像这样: production: sessions: default: database: my_app_production hosts: – localhost:27017 options: read: primary static_content: database: static_content_db hosts: – localhost:27017 options: read: primary options: use_utc: true 我有static_content会话的特殊模型,它们看起来像这样: class StaticMappings include Mongoid::Document include Mongoid::Attributes::Dynamic store_in collection: “static_mappings”, session: “static_content” end 我想防止自己形成意外调用StaticMappings.destroy_all或StaticMappings.create(…) 。 这可能吗? 我发现这使用Mongoid将整个模型设为只读 ,但这不会阻止某人在模型实例上调用create或destroy 。

如何使用Mongoid更新Rails中的嵌套Mongo文档属性?

(如果这个问题的细节很短,请提前道歉,我会看评论并添加我能做的) 我有一个带有以下内容的模型: class Product include Mongoid::Document include Mongoid::Timestamps #… field :document_template, :type => Document accepts_nested_attributes_for :document_template 在document document_template中,是以下references_many,我想修改它。 具体来说,我想更改引用的字体: class Document include Mongoid::Document include Mongoid::Timestamps #… references_many :fonts, :stored_as => :array, :inverse_of => :documents 我应该在控制器和表单中使用什么样的逻辑和细节来完成这项工作? 如果您希望我添加一些我尝试过的东西,请发表评论; 但是,我没有任何运气。 以下是使用rails console快速显示的问题: # Grab a Product and check how many fonts are in it’s document_template ruby-1.8.7-p302 > prod […]

mongodb mongoid-rails无法在嵌入文档的散列中增加计数器

我使用的是Mongoid 4.0.2,Rails 4.2.1和Mongodb 2.4.8。 我将daily_events作为人员集合中的嵌入文档,如下所示。 class Person include Mongoid::Document field :email, type: String field :month, type: Date field :monthly_total, type: Hash, default: {:email_open => 0, :email_click => 0, :page_view => 0} embeds_many :daily_events end class DailyEvent include Mongoid::Document field :name, type: String field :day_1, type: Hash, default: -> {:email_open => 0, :email_click => 0, :page_view […]

belongs_to vs belongs_to_related,has_many vs has_many_related

_related为关系添加了什么? 例如,belongs_to和belongs_to_related有什么区别? 我主要在Mongoid应用程序中看到这个,但不确定它是否也适用于Rails。

Ruby on Rails Mongoid和Webfaction:未授权查询(错误16550)

我使用的是2.4.4版,遵循http://docs.webfaction.com/software/mongodb.html上的程序,并使用Mongoid和Ruby on Rails。 我还在我正在使用的db中使用“userAdminAnyDatabase”权限创建了一个用户,并将其与此rails mongoid配置一起使用: production: sessions: default: database: hosts: – localhost: username: password: 我有服务器使用–auth标志运行,我也尝试使用mongodb cpmmand行的用户,它工作但我部署了我的Rails应用程序,我仍然得到: 失败,错误16550:“未授权查询 有什么我想念的吗? 也许我需要创建一个特殊的用户?

Mongoid`group()`条件

我想为Mongoid中的分组提供条件,但是如何在条件哈希中为属性发送多个值? 这就是我想要做的: PageViews.collection.group( cond: {page_id: [‘4e6912618083ab383e000010’, ‘4e6912618083ab383e000009’]}, key: ‘timestamp’, initial: {count: 0}, reduce: “function(x, y) {y.count += x.count;}” ) 因此,任何带有page_id PageView都将是查询的一部分,但我似乎无法使条件哈希( cond )完全起作用! 我在这里做错了什么,我真的很困惑。 以下是group()方法的API文档: http : //api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method 任何帮助将不胜感激。 更新 像这样运行查询: PageViews.collection.group( cond: {page_id: { $in : [‘4e6912618083ab383e000010’, ‘4e6912618083ab383e000009’]}}, key: ‘timestamp’, initial: {count: 0}, reduce: “function(x, y) {y.count += x.count;}” ) 返回以下错误,但语法对我来说很好: SyntaxError: (irb):170: syntax […]

Ember模型仅加载最后一条记录

我正在尝试重现Railscasts 410示例(称为Raffler ),更改最新版本的设置并符合我的习惯: Ember 1.0.0-rc.6 Rails 4.0.0 Mongoid大师(4.0) 哈姆尔4 会徽0.3.0 在这个示例项目中,我们创建了一个简单的模型Entry ,它调用一个小的Rails Rest API。 一切都按预期工作,除了调用Raffler.Entry.find()获取所有条目只加载最后一条记录。 这是我的模型: Raffler.Entry = DS.Model.extend name: DS.attr(‘string’) winner: DS.attr(‘boolean’) 我的商店: DS.RESTAdapter.configure(‘plurals’, entry: ‘entries’) Raffler.Store = DS.Store.extend revision: 12 adapter: DS.RESTAdapter.create() 当调用Raffler.Entry.find() ,在http://localhost:3000/entries上有一个AJAX请求,并且返回所有记录(所以我不认为问题是服务器端): {“entries”:[{“id”:{“$oid”:”51e5b35b492cd4d286000001″},”name”:”Foo”,”winner”:true},{“id”:{“$oid”:”51e5b35b492cd4d286000002″},”name”:”Bar”,”winner”:false},{“id”:{“$oid”:”51e5b384492cd4d286000003″},”name”:”Baz”,”winner”:true}]} 但是只有最后一条记录真正加载到模型中。 这里是JS控制台: e=Raffler.Entry.find() e.toArray().length => 1 e.objectAt(0).get(‘name’) => “Baz” (always the last one) e.objectAt(1) => undefined

在Mongoid中find_or_create_by是否安全?

我有一个使用Mongoid的find_or_create_by方法的Web应用程序。 poll = Poll.find_or_create_by(fields) 在我们投入生产之前,我正在尝试运行故障情况,并且我发现多个用户可以尝试使用此方法访问此资源。 是否有可能创建同一对象的多个实例? 我该怎么做才能防止这种情况发生?