0
votes

Still working on my Rails/MongoDB app, I have yet another problem. This time, I can create embedded documents, but can't delete them, though I've been doing what has been said in another Stackoverflow topic (http://stackoverflow.com/questions/3693842/remove-an-embedded-document-in-mongoid)

Here goes my controller :

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

The weirdest part is that everything seems to go well : success is equal to true in the rendered JSON object. I also tried to replace "success = @feed.first.save" with "@self.save" : in that case, it returns false, but with no further explanations.

(I do know that for the logic behind this controller to be perfect, I should loop on the @feed array, and I will once it starts working ^^ it's just easier to debug that way)

So, is there any way I may find out why @ßelf.save fails, or why @feed.first.save doesn't fail but doesn't actually save either ?

Thanks.

1

1 Answers

0
votes

Here is what you're doing as I see it

You are using FeedSubscriptionsController to delete the object with id if @feed.first, then you try and save @feed.first, but @feed.first points to an already deleted object, so it's failing to save it.