gpt4 book ai didi

ruby-on-rails - rails 更新方法不起作用

转载 作者:行者123 更新时间:2023-12-05 07:55:34 25 4
gpt4 key购买 nike

我正在尝试更新 b 对象。每当我运行更新方法时,它都会更新 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(书签模型类)

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

最佳答案

如果对象以任何方式无效,则 Update 方法将失败。请参阅:http://apidock.com/rails/v4.1.8/ActiveRecord/Persistence/update

您是否尝试过检查是否存在任何阻止保存发生的错误?尝试通过放置或在更新发生之前粘贴 binding.pry 来检查 b.errors。

关于ruby-on-rails - rails 更新方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com