Tag: mongoid

mongoid – mongodb,mapreduce获取范围可变或将其传递给输出

在mapreduce中,我有一个范围total_count,用于在分组内容后计算一些东西。 如何在输出中恢复该范围可变? 编辑: map = %Q{ function() { ttotal_commission += this.commission; tuniq_commission += this.commission; // add first if it not goto reduce emit({ip: this.ip, campaign_id: this.campaign_id}, {commission: this.commission, abc: this.commission}); } } reduce = %Q{ function(key, values) { tuniq_commission += values[0].commission; // add 1 and substract laster for uniq var total_commission = 0; values.forEach(function(value) { […]

Mongoid Association创建(不需要的)记录

我不知道Mongoid为何在协会创造新纪录。 我正在密切关注代码,但我从来没有见过这样的东西。 我做了一个测试并减少了代码。 我离开录像机以防它可能是相关的。 它“不应该为MT创建重复的条目”do state = PolcoGroup.create(type :: state,name:’MT’,active:true)s = state.get_senators state.junior_senator = s [:state_junior_senator]#!! ! 这会创建一个新记录state.senior_senator = s [:state_senior_senator]#!!!!! 所以这行预期(Legislator.all.size).to eql(2)#实际上等于4 – 每个关联创建一个新的记录最终结果是:Legislator.all.map(&:sortname)=> [“Tester,乔恩(参议员)[D-MT]“,”沃尔什,约翰(参议员)[D-MT]“,”沃尔什,约翰(参议员)[D-MT]“,”测试员,乔恩(参议员) [D-MT]“] ##模型类PolcoGroup包括Mongoid :: Document包括Mongoid :: Timestamps包括VotingMethods包括DistrictMethods扩展DistrictClassMethods包括StateMethods字段:name,:type => String … #STATE RELATIONSHIPS —– ———————— has_one:junior_senator,class_name:“Legislator”,inverse_of :: jr_legislator_state has_one:senior_senator,class_name:“Legislator”,inverse_of: :sr_legislator_state … end class立法者包括Mongoid :: Document包括Mongoid :: Timestamps#以下字段直接来自govtrack字段:govtrack_id,类型:整数字段:bioguideid,类型:字符串… belongs_to:jr_legislator_state,class_name:“ PolcoGroup“,inverse_of :: junior_senator […]

FactoryGirl创建了不完整的模型

假设我有一个城市模型,其中: class city field :full_name, type: String # San Francisco, CA, United States field :_id, type: String, overwrite: true, default: ->{ full_name } end 假设我在/spec/factories/cities.rb中定义了一个工厂: FactoryGirl.define do factory :city do full_name ‘San Francisco, CA, United States’ end end 在其中一个规范中运行以下代码: city_attrs = { full_name: ‘San Francisco, CA, United States’ } City.create! city_attrs => # FactoryGirl.create(:city) => […]

基于Mongoid计算字段的查询

我想在模型Invoice实施一个overdue范围,以返回超过日期的所有发票,直到他们必须付款为止。 我有字段invoice_date, :type => Date和days_for_payment, :type => Integer 。 在我之前的版本(基于ActiveRecord构建)中,我可以使用该查询 Invoice.where(“invoice_date + days_for_payment < ?", Date.today) 此查询在DB端进行了计算。 有没有办法让Mongoid完成同样的事情? 或者有没有人知道一个好的解决方法(proc,lambda等)? 我使用mongoid’2.4.12′

有时候在monogid中删除文档

我正在使用mongoid的rails app。 这是模型(集合)之一: class Document include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paperclip field :time, type: Time field :radius, type: Float attr_accessible :time,:radius end 时间字段包含应删除模型的实际时间。 我怎么能这样做,我有一个想法,我必须写一个脚本把它放在cron工作。 但我不想创建一个cron工作。 有没有其他方法可以自动化这个或我在轨道模型中的任何方法我可以定义或仅内置到rails中的东西。 我肯定错过了一些东西。

Rails mongoid动态字段 – 没有方法错误

ENV: Rails的3.2.12 ruby1.9.3 Mongoid-3.1.1 我有模特: class Item include Mongoid::Document field :name, type: String field :type, type: String end 但如果我尝试在视图中添加动态字段,让我们说“颜色”,我得到一个未定义的方法错误。 allow_dynamic_fields: true在配置文件中启用allow_dynamic_fields: true 。 _form.html.erb: 如果我尝试编辑已经具有颜色属性的项目,一切正常。 我需要添加几个动态属性,这取决于item.type但没有这样的东西: 编辑: 错误: Item#new中的NoMethodError 显示/app/views/items/_form.html.erb,其中第31行引发: #dextracted源的未定义方法`color’(第31行附近): 28: 29: 30: 31: 32: 33: 34:

使用mongodb作为数据库的rails中的关联

我正在使用设计。 它将当前用户标识赋予 current_user.id 有很多用户。 控制器名称为empsals_controller.rb class EmpsalsController < ApplicationController def index @empsals = Empsal.all end def show @empsal = Empsal.find(params[:id]) end def new @empsal = Empsal.new end def edit @empsal = Empsal.find(params[:id]) end def create @empsal = Empsal.new(params[:empsal]) respond_to do |format| if @empsal.save format.html { redirect_to @empsal, notice: 'Empsal was successfully created.' } format.json { […]

to_json中的Mongoid虚拟属性

我试图让一些虚拟(非持久)属性显示在一些Mongoid模型的JSON表示中,但似乎无法让它工作: class MyModel include Mongoid::Document def virtual_attribute @my_attribute || false end def virtual_attribute=(value) @my_attribute=value end end class MyController def myaction false_values=MyModel.where( whatever ) true_values=MyModel.where( something_else ).map{ |model| model.virtual_attribute=true } @val[‘my_models’]=false_values+true_values render json: @val.to_json( :include => {:my_models => {:methods => %w(virtual_attribute)}} ) end end virtual_attribute不会出现在json中。 我究竟做错了什么? 编辑 – 好吧,所以我想我的实际问题是我无法弄清楚如何在嵌套在根对象中的每个对象数组上调用virtual_attribute方法。

Rails g为mongoid命令失败

我试图使用ruby-1.9.2使用以下命令从rails 3.2应用程序初始化mongoid: rails g mongoid:config 当我发出命令时,我收到以下错误: /Users/paulcowan/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/rubygems_integration.rb:143:in block in replace_gem’: railties is not part of the bundle. Add it to Gemfile. (Gem::LoadError) from /Users/paulcowan/.rvm/gems/ruby-1.9.2-p290/bin/rails:18:in block in replace_gem’: railties is not part of the bundle. Add it to Gemfile. (Gem::LoadError) from /Users/paulcowan/.rvm/gems/ruby-1.9.2-p290/bin/rails:18:in block in replace_gem’: railties is not part of the bundle. Add it to Gemfile. […]

ActionView :: Template :: Error(在heroku上没有将Fixnum隐式转换为String

我正在使用Mongodb.App正在使用ROR应用程序在localhost上正常工作,但是当我在heroku中部署它时,我收到错误。 我的一个视图文件命令是这样的: @h=Portfolio.last.folders Portfolio是一个将字段文件夹作为数组的集合。 生产中的Mongodb包含这个。 即使我尝试在heroku rails控制台中做同样的事情我也得到了同样的错误: TypeError: no implicit conversion of Fixnum into String from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/protocol/commands/authenticate.rb:35:in `+’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/protocol/commands/authenticate.rb:35:in `digest’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/protocol/commands/authenticate.rb:47:in `build_auth_command’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/protocol/commands/authenticate.rb:25:in `initialize’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:488:in `new’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:488:in `login’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:48:in `block in apply_auth’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:47:in `each’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:47:in `apply_auth’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/cluster.rb:250:in `with_secondary’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/session/context.rb:104:in `with_node’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/session/context.rb:43:in `query’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/query.rb:115:in […]