mhopkins321
mhopkins321

Reputation: 3083

Create "userName" column that is based off of first and last name

I am using devise to generate my users. I have a first_name column and a last_name column. Upon submitting the form I'd like for username to become the first letter of the first_first name, plus the last name.

first_name: Michael
last_name: Hopkins
user_name: mhopkins

I am very new to rails and am not sure where I put this logic. Can anybody help out?

Upvotes: 1

Views: 929

Answers (1)

HungryCoder
HungryCoder

Reputation: 7616

Simplest way. Put this in your User (or appropriate) model

before_create :prepare_username

def prepare_username
 self.user_name = (self.first_name[0] + self.last_name).to_s.downcase
end

Note: please make sure you first check first_name and last_name is not empty

Upvotes: 2

Related Questions