I'm not sure if I'm supposed to do this transaction in controller or model since I have to calculate and change other user's column value.
In my understanding, stating in model is only for changing the value of itself(user-self here)
Existing user(ID:1367) can send this url to his friends as an invitation.
http://example.com/users/sign_up?invitation=1367
Then Users table has hidden column called invitation
.
and This is procedure how it works that I want.
His friend input information to sign up.
He hits submit button then hidden field 'invitation' will be also sent to form.
1367
will be set in the column called 'invitation' of the record.He will receive confirmation mail, and when he clicks on the link, I'd like to add this transaction, and execute only once for his account.
Of course, this shouldn't be executed when the existing user tried to re-activate.
Only for the first confirmation for the new user.
code
@user = User.find_by_invitation(current_user.invitation)
@user.friends = @user.friends + 1
@user.save
I already have registration controller that helps adding extra transaction to Devise.
Now I want to know how I can implement this procedure to my app.
Should it be something like this?
registrations_controller.rb
def after_????????
if @user = User.find_by_invitation(current_user.invitation)
@user.friends = @user.friends + 1
@user.save
end
end