我無法將我的文檔保存爲mongoid。我在我的控制器下面的代碼片段:查找後的Mongoid文件持久性
params[:user][:residence_attributes][:locations_attributes].each do |num,location_attributes|
zipcode = Location.find(location_attributes[:id])
if !zipcode.update_attributes(location_attributes)
puts "fail"
fail = true
end
puts "zipcode again #{zipcode}"
puts "zipcode number #{zipcode.number}"
puts "zipcodes = #{Zipcode.count}"
zipcode = Zipcode.find(@user.residence.locations[0].id)
puts "zipcode again #{zipcode}"
puts "zipcode number #{zipcode.number}"
puts "zipcodes = #{Zipcode.count}"
zipcode = Zipcode.find(@user.residence.locations[0].id)
puts "zipcode again #{zipcode}"
puts "zipcode number #{zipcode.number}"
puts "zipcodes = #{Zipcode.count}"
end
而且它產生的輸出如下:
zipcode again #<Zipcode:0x000000063826a0>
zipcode number 11210
zipcodes = 1
zipcode again #<Zipcode:0x00000006348860>
zipcode number
zipcodes = 1
zipcode again #<Zipcode:0x00000006340ef8>
zipcode number
zipcodes = 1
所以,問題是爲什麼在郵編ID更改當我找到這個文件的第二和第三時間?
這是一個問題,因爲文檔不會保留。
我有以下型號:
class Zipcode < Location
include Mongoid::Document
attr_accessible :number
attr_accessor :number
validates_presence_of :number
validate :check_zipcode
end
class Location
include Mongoid::Document
attr_accessible :latitude, :longitude
belongs_to :locatable, polymorphic: true
end
class UserResidence
include Mongoid::Document
has_many :locations, as: :locatable
embedded_in :user, :inverse_of => :residence
attr_accessible :locations_attributes
accepts_nested_attributes_for :locations, autosave: true
#validates_presence_of :locations
#validates :locations, :length => {:minimum => 1}
end
感謝
自動保存是真的,我會用user_residence模型更新帖子。無論哪種方式,我直接更新郵編,所以我仍然需要自動保存? – GTDev
在Mongoid中也有一個相關的Bug:https://github.com/mongoid/mongoid/issues/1198 – Tilo
這個bug還在mongoid 2.2.1中嗎?因爲這是我使用的版本 – GTDev