0
我一直试图找一个合适的命令做一个upsert,但我失败了。Rails 4 MongoID Upsert尝试
这里是我尝试:
csv.each do |row|
inner = {}
if row['PN'] != 'TOTAL'
time = Time.now
stamp = time.strftime("%F")
inner = {
'PN' => row['PN'],
'thisWkUse' => row['this WK Usage'], 'thisWkFail' => row['this WK Failure'], 'thisWkFailPercent' => row['this WK Failure%'].gsub('%',''),
'prev4WkUse' => row['Previous 4 WK Usage'], 'prev4WkFail' => row['Previous 4 WK Failure'], 'prev4WkFailPercent' => row['Previous 4 WK Failure%'].gsub('%',''),
'Platform' => row['Platform'].gsub('%',''),
'created' => stamp
}
@report = Report.find(PN: inner['PN'], Platform: inner['Platform']).update(inner,[upsert: true ])
@report.upsert()
end
end
这是我看到的错误:
MOPED: someip:someport QUERY database=reidb collection=reports selector={"_id"=> {:PN=>"7056733", :Platform=>"CALLISTO_PLUS"}} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 92.2640ms
Completed 500 Internal Server Error in 447ms
NoMethodError (undefined method `upsert' for nil:NilClass):
app/controllers/reports_controller.rb:54:in `block in upload'
app/controllers/reports_controller.rb:42:in `upload'
这里是模型:的
class Report
include Mongoid::Document
field :_id, type: Object
field :PN, type: String
field :thisWkUse, type: Integer
field :thisWkFail, type: Integer
field :thisWkFailPercent, type: Float
field :prev4WkUse, type: Integer
field :prev4WkFail, type: Integer
field :prev4WkFailPercent, type: Float
field :Platform, type: String
field :created, type: Date
index({ _id: 1,PN: 1, Platform: 1 }, { unique: true, background: true , drop_dups: true})
end
您想要为查询使用'where'方法(而不是'find'),请参阅http://mongoid.org/en/mongoid/docs/querying.html – 2014-09-06 13:23:09
尝试过的报告= Report.where(:PN => inner ['PN'],:Platform => inner ['Platform']).new(inner)和reports.upsert,并在第二次导入时获得重复记录。 – 2014-09-06 23:02:02
请尝试:@report = Report.where(PN:inner ['PN'],Platform:inner ['Platform'])。upsert(inner) – 2014-09-08 17:19:31