Hi,
i despair in case of an array.
I’ll take 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 werden 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
Hi,
i despair in case of an array.
I’ll take 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.
You’re deleting from the array as you are iterating over it - don’t do
that.
Fred
puts task.id
else #Wenn Subtasks angezeigt werden sollen
Hi,
i despair in case of an array.
I’ll take 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.
You’re deleting from the array as you are iterating over it - don’t do
that.
Yep. A common mistake when iterating arrays. One solution is to build a
new array containing the objects you want, rather than deleting objects
that you don’t want. There may be better approaches that might use less
memory though.
Yep. A common mistake when iterating arrays. One solution is to build a
new array containing the objects you want, rather than deleting objects
that you don’t want. There may be better approaches that might use less
memory though.
And if you do want to do that, a rubyish way of doing that is to use
methods like select
The relevant portion of the code above could be written as