Help me refactoring this nasty Ruby if/else statement

Posted by Suborx on Stack Overflow See other posts from Stack Overflow or by Suborx
Published on 2010-05-08T11:54:49Z Indexed on 2010/05/08 11:58 UTC
Read the original article Hit count: 252

Filed under:
|

Hello, so I have this big method in my application for newsletter distribution. Method is for updating rayons and i need to assigned user to rayon. I have relation n:n through table colporteur_in_rayons witch have attributes since_date and _until_date.

I am junior programmer and i know this code is pretty dummy :) I appreciated every suggestion.

def update rayon = Rayon.find(params[:id]) if rayon.update_attributes(params[:rayon]) if params[:user_id] != "" unless rayon.users.empty? unless rayon.users.last.id.eql?(params[:user_id]) rayon.colporteur_in_rayons.last.update_attributes(:until_date => Time.now) Rayon.assign_user(rayon.id,params[:user_id]) flash[:success] = "Rayon #{rayon.name} has been succesuly assigned to #{rayon.actual_user.name}." return redirect_to rayons_path end else Rayon.assign_user(rayon.id,params[:user_id]) flash[:success] = "Rayon #{rayon.name} has been successfully assigned to #{rayon.actual_user.name}." return redirect_to rayons_path end end flash[:success] = "Rayon has been successfully updated." return redirect_to rayons_path else flash[:error] = "Rayon has not been updated." return redirect_to :back end end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby