Help simplifying code by removing some if's

I need some help simplifying some code. Some ago, I was trying to validate a User model depending on the action they were performing.

For example: 1) Signing up (validate presence of username) 2) Inviting someone to user the app (DO NOT validate presence of username    because invited user is supposed to pick his username) 3) Accepting invitation link (validate presence of username)

So Frederick Cheung (thanks Frederick) suggested I validate depending on the user state. So I create a user_state attribute for the User model.

I need some help simplifying some code. Some ago, I was trying to validate a User model depending on the action they were performing. Validation depends on Controller - Rails - Ruby-Forum

For example: 1) Signing up (validate presence of username) 2) Inviting someone to user the app (DO NOT validate presence of username   because invited user is supposed to pick his username) 3) Accepting invitation link (validate presence of username)

So Frederick Cheung (thanks Frederick) suggested I validate depending on the user state. So I create a user_state attribute for the User model.

-------------------------------------------

The following is for ACCEPTING INVITATION LINK. An admin invites an user. The user is created with user_state="invited". The invited user receives an email with a link. The link takes him to a form to pick his username and password.

This is the code I came up with. It works, but I have a lot of if's statements and I know there should be a cleaner way to do it.

Can anyone give me any suggestions as to how to simplify it? I'm a newbie, please understand hehe, thanks :slight_smile: ---------------------------------------- user.rb ---------------------------------------- validates :username, :presence => true, :uniqueness => true, :if => :are_you_activating_your_account?, :on => :update

def are_you_activating_your_account? if self.user_state=="invited"    true else    false end end

I'll leave the rest for others more experienced, but this leaped out at me. The previous can be written as this:

def are_you_activating_your_account?    self.user_state == 'invited' end

Walter

Thanks, it did simplify it. Any suggestions on the controller?

Leonel *.* wrote in post #1014810:

Thanks, it did simplify it. Any suggestions on the controller?

if @user.update_attributes(params[:user])       # if passed validation,       # set user state to confirmed       @user.user_state = "confirmed"       @user.confirmed_at = Time.now       if @user.update_attributes(params[:user])

Can that last if statement ever be false?