I am trying to have my application automatically generate usernames to be used as a url. As a rule, they will add the user name and surname, however, when a user with the same name and surname already exists, he will add a number to the name, which increases for each.
This is the method I created:
def full_name first_name + ' ' + last_name end def user_name t_user_name = first_name.downcase.strip.gsub(' ', '').gsub(/[^\w-]/, '') + last_name.downcase.strip.gsub(' ', '').gsub(/[^\w-]/, '') user_name = t_user_name num = 1 while User.find_by_user_name(user_name).count > 0 num += 1 user_name = "#{t_user_name}#{num}" end end
I am currently getting the error message:
undefined method `find_by_user_name'
What would I think would work automatically?
I also tried to give examples shown in this post:
create a unique username (omniauth + devise)
but I kept getting the error:
Mysql2::Error: Unknown column 'users.login' in 'where clause': SELECT COUNT(*) FROM `users` WHERE `users`.`login` = 'bobweir'
Although I added
t.string :login, null: false, default: ""
to users table
edit2: schema.rb
ActiveRecord :: Schema.define (version: 20150611033237) do
create_table "users", force: :cascade do |t| t.string "first_name", limit: 255 t.string "last_name", limit: 255 t.string "email", limit: 255, default: "", null: false t.string "encrypted_password", limit: 255, default: "", null: false t.string "reset_password_token", limit: 255 t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", limit: 4, default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip", limit: 255 t.string "last_sign_in_ip", limit: 255 t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end