Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Can';t使用Mongoid删除嵌入元素_Ruby On Rails_Mongodb_Mongoid - Fatal编程技术网

Ruby on rails Can';t使用Mongoid删除嵌入元素

Ruby on rails Can';t使用Mongoid删除嵌入元素,ruby-on-rails,mongodb,mongoid,Ruby On Rails,Mongodb,Mongoid,仍然在使用Rails/MongoDB应用程序,我还有一个问题。 这一次,我可以创建嵌入文档,但不能删除它们,尽管我一直在做另一个Stackoverflow主题中提到的事情(http://stackoverflow.com/questions/3693842/remove-an-embedded-document-in-mongoid) 这是我的控制器: class FeedSubscribtionsController < ApplicationController has_to_be

仍然在使用Rails/MongoDB应用程序,我还有一个问题。 这一次,我可以创建嵌入文档,但不能删除它们,尽管我一直在做另一个Stackoverflow主题中提到的事情(http://stackoverflow.com/questions/3693842/remove-an-embedded-document-in-mongoid)

这是我的控制器:

class FeedSubscribtionsController < ApplicationController
  has_to_be_connected

  def create
    if session[:user_id] != params[:id]
      @self        = current_user
      attributes   = { :user => @self, :userId => params[:id], :feedId => params[:feed] }
      subscribtion = FeedSubscribtion.create attributes
      success      = subscribtion.save
      render json: { :success => success, :feed => params[:feed] }
    end
  end

  def destroy
    success     = false
    if session[:user_id] != params[:id]
      @self     = current_user
      uid, fid  = params[:id], params[:feed]
      @feed     = @self.feed_subscribtions.where :userId => uid, :feedId => fid
      if @feed.count > 0
    @self.feed_subscribtions.delete @feed.first.id.to_s
        success = @feed.first.save
      end
    end
    render json: { :success => success, :feed => params[:feed] }
  end
end
class FeedSubscribtionsController@self,:userId=>params[:id],:feedId=>params[:feed]}
subscribtion=FeedSubscribtion.create属性
success=subscribtion.save
呈现json:{:success=>success,:feed=>params[:feed]}
结束
结束
def销毁
成功=错误
如果会话[:用户id]!=参数[:id]
@self=当前用户
uid,fid=params[:id],params[:feed]
@feed=@self.feed\u subscriptions.where:userId=>uid,:feedId=>fid
如果@feed.count>0
@self.feed\u subscriptions.delete@feed.first.id.to\u
success=@feed.first.save
结束
结束
呈现json:{:success=>success,:feed=>params[:feed]}
结束
结束
最奇怪的是,一切似乎都进展顺利:在呈现的JSON对象中,成功等于真。 我还尝试将“success=@feed.first.save”替换为“@self.save”:在这种情况下,它返回false,但没有进一步的解释

(我确实知道,为了使这个控制器背后的逻辑更加完美,我应该在@feed数组上循环,一旦它开始工作^^^这样调试就更容易了)

那么,我有没有办法找出@ßelf.save失败的原因,或者@feed.first.save没有失败但实际上也没有保存


谢谢。

在我看来,这是你正在做的事情

您正在使用FeedSubscriptionsController删除id为@feed.first的对象,然后尝试保存@feed.first,但@feed.first指向已删除的对象,因此无法保存该对象