Hi.

I have the following controller of the update action:

[code]
def update
    @projeto = Projeto.find(params[:id])

    if (@projeto.update_attributes(params[:projeto]))

    ## Mesmo raciocínio utilizado no create.
    @permissaoA = Permissao.find(:first, :conditions => ["usuario_id =
?", @projeto.responsavel.to_i])
    @permissaoAvancado1 = Permissao.find(:first, :conditions =>
["projeto_id = ? and usuario_id = ?", 0, @projeto.responsavel.to_i])
    if @permissaoAvancado1.nil?
    ## Caso tenham excluido os usuários responsáveis, as permissões
seriam nulas. Dai ele verifica se vai alterar ou criar uma permissão
    if @permissaoA.nil?
    @permissaoAv = Permissao.new(params[:permissao])
    @permissaoAv.perfil = "Avançado"
    @permissaoAv.usuario = Usuario.find(:first, :conditions => ["id =
?", @projeto.responsavel.to_i])
    @permissaoAv.save(params[:permissao])
    end
    end

    ## O mesmo que avançado
    @permissaoI = Permissao.find(:first, :conditions => ["projeto_id = ?
and perfil = ? and usuario_id = ?", @projeto.id, "Intermediário",
@projeto.responsavel_td.to_i])
    @permissoesB = Permissao.all(:conditions => ["projeto_id = ? and
usuario_id = ? and perfil = ?", @projeto.id,
@projeto.responsavel_td.to_i, "Básico"])
    @permissaoAvancado2 = Permissao.find(:first, :conditions =>
["projeto_id = ? and usuario_id = ?", 0, @projeto.responsavel_td.to_i])
    if @permissaoAvancado2.nil?
    if @permissaoI.nil?
      @permissaoIn = Permissao.new(params[:permissao])
#    else
#      @permissaoIn = @permissaoI
#    end
    @permissaoIn.usuario = Usuario.find(:first, :conditions => ["id =
?", @projeto.responsavel_td.to_i])
    @permissaoIn.projeto = Projeto.find(:first, :conditions => ["id =
?", @projeto.id])
    @permissaoIn.perfil = "Intermediário"
#    if [email protected]?
#    @permissaoIn.update_attributes(params[:permissao])
#    else
    @permissaoIn.save(params[:permissao])
    if [email protected]?
      for perm in @permissoesB
        perm.destroy
      end
    end
    end
    end

    @permissaoB = Permissao.find(:first, :conditions => ["projeto_id = ?
and perfil = ? and usuario_id = ?", @projeto.id, "Básico",
@projeto.coordenador.to_i])
    @permissaoAvancado3 = Permissao.all(:conditions => ["projeto_id = ?
and usuario_id = ?", 0, @projeto.coordenador.to_i])
    @permissoesI = Permissao.all(:conditions => ["projeto_id = ? AND
usuario_id = ? AND perfil = ?", @projeto.id, @projeto.coordenador.to_i,
"Intermediário"])
    ## Se ele já tiver a permissao de avançado, não faz nada. Se não
tiver, cria uma (sem nenhum projeto)
    if @permissaoAvancado3.empty? && @permissoesI.empty?
#    if @permissaoB.nil?
      @permissaoBa = Permissao.new(params[:permissao])
#    else
#      @permissaoBa = @permissaoB
#    end
    @permissaoBa.usuario = Usuario.find(:first, :conditions => ["id =
?", @projeto.coordenador.to_i])
    @permissaoBa.projeto = Projeto.find(:first, :conditions => ["id =
?", @projeto.id])
    @permissaoBa.perfil = "Básico"
#    if [email protected]?
#    @permissaoBa.update_attributes(params[:permissao])
#    else
      @permissaoBa.save(params[:permissao])
#    end
    end


    ## Ao editar um projeto, existe a possibilidade da alteração do
responsável pela Td
    ## Portanto, ele procura por todas as tarefas daquele projeto
    @tarefas = Tarefa.all(:conditions => ["projeto_id = ?",
@projeto.id])
    ## Varre cada uma
    for tarefa in @tarefas
    ## Altera o campo "responsável" pelo respnsável_td recém modificado
(ou não) na edição do projeto
      tarefa.responsavel = Usuario.find(:first, :conditions => ["id =
?", @projeto.responsavel_td]).nome
    ## Atualiza as alterações
      tarefa.update_attributes(params[:tarefa])
    end
        flash[:msg] = "Projeto atualizado com sucesso"
        redirect_to(@projeto)
   else
      render :action => "edit"
    end
  end
[/code]

It works ok, but what i wanna do is:

destroy the "permissao" of "responsavel_td" and "coordenador" BEFORE the
update.
in other words, if the "responsavel_td" or the "coordenador" change, i
wanna to destroy the "permissao" existing for those who were before the
change.

I make my self clear?

If you can't understand me, just think this way.. How can I return the
value before the update?

Thanks any help.
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to