我试图更新b对象。每当我运行update方法时,它都会更新b对象,但不会影响数据库。我试图找到解决方案,但我无法找到它的解决方案。请告诉我我做错了什么,如果你们有任何人认为这个问题能够找到它的解决方案,请将该解决方案提交给我。
data = params[:d]
#data1 = params[:d1]
data2 = data1
request_bookmarked = Request.getRequest(data)
bookmarked_against_Request = Request.first
request_bookmarked_2 = request_bookmarked
bookmarked_against_Request_2 = bookmarked_against_Request
b = Bookmark.where(request_id: bookmarked_against_Request[:_id]).first()
if b != nil
flag = false
ind = 0;
b[:corsponding_requests].each.with_index do |t , index|
if t == request_bookmarked[:id]
flag = true
ind = index
end
end
if flag == true
b1 = b
#b1.delete_at(ind)#request_bookmarked[:_id])
b1[:corsponding_requests].delete(request_bookmarked[:id])
bookmarked_against_Request_2[:favourites].delete(request_bookmarked[:_d])
request_bookmarked[:favourites_of].delete(bookmarked_against_Request[:_id])
b2 = Hash.new
b2['corsponding_requests'] = Array.new
b2['corsponding_requests'].push(*b1[:corsponding_requests])
#b.update(Hash['corsponding_requests' , b1[:corsponding_requests]])
if b.update_attributes(b2)
flash[:notice] = "Request has been un bookmarked ."
end
Bookmark.rb(Bookmark Model Class)
class Bookmark
include Mongoid::Document
include Mongoid::Timestamps
field :owner_req, type: String
field :request_id, type: String
field :corsponding_requests, type: Array
class << self
def createBookmark ( title)
bookmark = self.new(title);
if bookmark.save
bookmark
end
end
end
end
答案 0 :(得分:1)
如果对象以任何方式无效,Update方法将失败。请参阅:http://apidock.com/rails/v4.1.8/ActiveRecord/Persistence/update
您是否尝试过检查是否存在阻止保存发生的错误?尝试通过put'ing或在更新发生之前粘贴binding.pry来检查b.errors。