开发者

Missing elements in an Ruby on Rails Array

开发者 https://www.devze.com 2022-12-15 04:42 出处:网络
i despair in case of an array. I put some data into an array, but when i iterate the array some elements of the array seems to be missed, but the missing element are

i despair in case of an array. I put some data into an array, but when i iterate the array some elements of the array seems to be missed, but the missing element are displayed in the view. I'm using the Plugin Rails_Authorization. my Code:

 def index
    if !params[:task_id] #Tasks der 1. ebene anzeigen, auf die der User zugriff hat
      @tasks = current_user.is_owner_of_what + current_user.is_moderator_of_what + current_user.is_user_of_what
      @tasks.uniq! #doppelte Einträge auf einen reduzieren
      puts @tasks
      @tasks.each do |task|
        puts task.id
        if !task.root? #ist Task kein wurzelelement?
          if (current_user.has_roles_for? Task.find(task.parent_id) ) #Hat der Benutzer Rechte auf Parent Task?
             @tasks.delete(task) #Objekt aus Array schmeißen da nur die Elemente der 1. Ebene dargestellt werde开发者_开发技巧n sollen, auf die der Benutzer Rechte hat

          end
        end
      end
    else #Wenn Subtasks angezeigt werden sollen
      @task=Task.find(params[:task_id])
      load_subtasks
      @tasks=@subtasks
    end
    #@tasks = Task.all
    respond_to do |format|

      format.html #index.html.erb
      format.xml  { render :xml => @tasks }
    end
  end

And My Debugging Log:

#<Task:0x1036e6ca8> 
#<Task:0x1036e3ee0> 
#<Task:0x1036df958> 
#<Task:0x1036dc190> 
#<Task:0x1036d8ea0> 
#<Task:0x1036d5138> 
#<Task:0x1036d18f8> 
#<Task:0x1036cbd90> 
5 
6 
8 
12 
14 


Try using the more concise #reject! syntax, since the #delete call sounds likely to cause issues.

 def index
    if !params[:task_id] #Tasks der 1. ebene anzeigen, auf die der User zugriff hat
      @tasks = current_user.is_owner_of_what + current_user.is_moderator_of_what + current_user.is_user_of_what
      @tasks.uniq! #doppelte Einträge auf einen reduzieren
      puts @tasks
      @tasks.reject! do |task|
        !task.root? && current_user.has_roles_for?(Task.find(task.parent_id))
      end
    else #Wenn Subtasks angezeigt werden sollen
      @task=Task.find(params[:task_id])
      load_subtasks
      @tasks=@subtasks
    end
    # ...
  end


You might want to try something along the lines of

@tasks.reject { |task| ... }

or

@tasks.delete_if { |task| ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号