Tag: mongoid

删除mongoid中的嵌入文档

我有一个只带有名称字段的项目模型,还有与line_items的嵌入关系。 class Project包含mongoid :: document字段:name embeds_many:line_items end class LineItem include mongoid::document field :title embedded_in :project, :inverse_of => :line_items end 我想这更像是mongo驱动程序的问题:如果我有这样的文件 db.project.find()[0] { _id : 123, name : “housework”, line_items:[ { title : “clean fridge”, _id : 601}, { title : “clean tub”, _id : 602}, { title : “clean oven”, _id : 603} ] } […]

Mongoid:如何防止通过批量分配创建未定义的字段?

这是代码: class M include Mongoid::Document field :name end params = { name: “foo”, age: 20 } M.create(params) #=> # 请注意, age未定义,但已保存。 这是有问题的(可能是DoS的来源),因为恶意用户可以在POST中添加任何参数,并且大字符串可以潜入未知字段。(例如name=foo&bogus=#{‘x’*1000000} ) 到目前为止,除了attr_accessible我找不到任何东西,但是对于Mongoid来说并不是很好,因为你必须在所有模型中attr_accessible在field和attr_accessible保持相同的字段名称。 不干。 我认为attr_accessible API非常适合ActiveRecord,因为有一个。 您没有明确定义模型(DRY)和b中的字段。 保证不存在将不存在的字段保存到RDB的可能性。 但是对于Mongoid,我认为应该有比attr_accessible更好的解决方案。 请注意,有一个全局配置设置allow_dynamic_fields但它不是关于质量分配,所以它超出了本讨论的范围,但我认为它实际上应该是一个每个模型的宏,并且还应该注意质量分配。 你是如何处理这个问题的?

即使使用限制,Rails Mongoid模型查询结果也会返回错误的大小/长度/计数信息

在我的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 我无法理解为什么会发生这种情况,它会一直显示食谱集合的总数,而且因为我使用了limit ,正确的值应该是100。 count = 0 recipes.each do |recipe| count += 1 end # WAT count # => 100 有人能帮助我吗? 谢谢! – Rails版本:3.2.3 Mongoid版本:2.4.10 MongoDB版本:1.8.4

何时索引,在Mongoid中索引什么?

我对索引有点新意,但我对索引的用例感到好奇。 (我假设它对索引字段的查询速度要快得多。)是否有标准来确定索引内容和索引时间? 我应该期待什么样的性能优势 – 特别是在Rails应用程序中使用Mongoid和MongoDb?

如何在Mongoid中更改文档的_type?

我在Rails应用程序中有以下模型: class User include Mongoid::Document … end class Admin < User … end 我得到一个用户: u = User.find(some_key) 并尝试更改_type: u._type # => “User” u._type = “Admin” u.save u._type # => “Admin” 但是,如果我重新加载对象,它仍然是一个用户: u.reload u._type = “User” 改变这个的正确方法是什么?

即使nested_attributes无效,update_attributes也始终返回true

我有2个嵌套数据模型: class Goodtender include Mongoid::Document include Mongoid::Timestamps field :name field :count references_many(:offerprices, :autosave => true) accepts_nested_attributes_for :offerprices, :allow_destroy => true, :reject_if => :all_blank validates_presence_of :name, :message => “Invalid” validates_numericality_of :count, :message => ‘Invalid’ validates_associated :offerprices, :message => ‘Invalid’ end class Offerprice include Mongoid::Document include Mongoid::Timestamps field :summ field :date_delivery, :type => DateTime field :note referenced_in […]

form_for中未定义的方法_s_path

我知道这有很多问题。 我环顾四周,但我似乎没有犯错误,我找到了答案。 我的锻炼资源的表单嵌套在用户之下,正在给出错误 undefined method `workouts_path’ for #<#:0x007f8891527b20> 那种forms: = form_for @workout do |f| … = f.submit Workouts Controller新动作(产生错误的地方): def new @user = User.find(params[:user_id]) @workout = @user.workouts.new end 锻炼模型以其独特的名字命名。 控制器是复数。 这是我的路线文件: resources :users do resources :workouts end 以及相关的佣金路线输出: user_workouts GET /users/:user_id/workouts(.:format) workouts#index POST /users/:user_id/workouts(.:format) workouts#create new_user_workout GET /users/:user_id/workouts/new(.:format) workouts#new edit_user_workout GET /users/:user_id/workouts/:id/edit(.:format) workouts#edit user_workout GET […]

ActiveRecord和Mogoid :: Document:association

我有一个基于ActiveRecord的模型,另一个基于Mogoid :: Document。 那可以一起做一个联想吗? 例如,2个型号: class User :destroy end class Avatar include Mongoid::Document field :file_name end 并检索用户的头像像这样: @user.avatar.file_name 谢谢 !

Heroku / Unicorn上的重复导轨错误 – “执行过期”,一个ActionView :: Template :: Error

我的问题类似于以下内容,但情况略有不同。 Rails:执行在time_zone_select上过期 我的设置是: Rails 3.2.13 Unicorn 4.6.2 Mongoid 3.0.22 轻便摩托车1.4.2 在Heroku Cedar上运行。 MongoDB在MongoLab上托管。 错误分批进行,并且通常由Heroku进程重启来解决。 第一个通常是下面的那个: An ActionView::Template::Error occurred in [controller]#[action]: execution expired vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/sockets/connectable.rb:46:in `read’ 以下是堆栈跟踪的最高位。 如果需要,很乐意添加更多! vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/sockets/connectable.rb:46:in `read’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/sockets/connectable.rb:46:in `block in read’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/sockets/connectable.rb:118:in `handle_socket_errors’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/sockets/connectable.rb:46:in `read’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/connection.rb:177:in `read_data’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/connection.rb:99:in `block in read’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/connection.rb:202:in `with_connection’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/connection.rb:97:in `read’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/protocol/query.rb:163:in `receive_replies’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/connection.rb:135:in `block in receive_replies’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/connection.rb:134:in `map’ vendor/bundle/ruby/1.9.1/gems/moped-1.4.2/lib/moped/connection.rb:134:in […]

ruby on rails 5.0.0.2不兼容并与gem mongoid冲突

ruby版本2.3.0 rails版本5.0.0.beta2 ** GEMFILE ** source ‘https://rubygems.org’ # Bundle edge Rails instead: gem ‘rails’, github: ‘rails/rails’ gem ‘rails’, ‘>= 5.0.0.beta2’, ‘ 5.1.0’ # Use Puma as the app server gem ‘puma’ # Use SCSS for stylesheets gem ‘sass-rails’, ‘~> 5.0’ # Use Uglifier as compressor for JavaScript assets gem ‘uglifier’, ‘>= 1.3.0’ # Use CoffeeScript […]