在设计中产生有意义的用户名 [英] generating meaningful usernames in devise

查看:98
本文介绍了在设计中产生有意义的用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的应用自动生成用作网址的用户名。通常,他们将用户名和姓氏相加,但是当已经存在具有相同姓名的用户时,它将附加一个号码给每个用户增加的名称。



这是我创建的方法:

  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

我正在收到错误:

 未定义的方法`find_by_user_name '

我以为会自动工作?



我也试图在这篇文章中显示的例子:



生成唯一用户名(omniauth + devise)



但是我不断收到错误:

  Mysql2 ::错误:'where子句'中的未知列'users.login':SELECT COUNT(*)FROM`users` WHERE` user`.`login` ='bobweir'

即使我添加了

  t.string:login,null:false,default:

给用户表



edit2:schema.rb



ActiveRe cord :: Schema.define(version:20150611033237)do

  create_tableusers,force::cascade do | t | 
t.stringfirst_name,limit:255
t.stringlast_name,limit:255
t.stringemail,limit:255,default:,null: false
t.stringencrypted_pa​​ssword,limit:255,default:,null:false
t.stringreset_password_token,limit:255
t.datetimereset_password_sent_at
t.datetimeremember_created_at
t.integersign_in_count,limit:4,default:0,null:false
t.datetimecurrent_sign_in_at
t.datetimelast_sign_in_at
t.stringcurrent_sign_in_ip,limit:255
t.stringlast_sign_in_ip,limit:255
t.datetimecreated_at,null:false
t.datetime updated_at,null:false
end

add_indexusers,[email],name:index_users_on_email,unique:true,using::btree
add_index用户,[reset_password_token],名称:index_users_on_reset_password_token,unique:true,using::btree

结束

解决方案

Rails通过在方法调用中推断出一个属性名称,实现了 find_by_sth ,从而返回一个ActiveRecordRelation。
$ b

由于您的模型中没有 user_name 属性,Active Record将无法对其进行搜索(只需将其转换为SQL查询即可) )。



另一方面,如果我是你,我会重新考虑你的 user_name 方法的逻辑。考虑在您的数据库中持续使用user_name。



顺便说一句,在提及的第二个错误中,请注意,您使用的示例是使用登录

I'm trying to have my app automatically generate usernames to be used as the url. Typically they will be the users first and last name added together however when there already exists a user with the same first and last name it will append a number to the name that 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'm currently getting the error:

undefined method `find_by_user_name'

Which i thought would work automatically?

I was also trying to examples shown in this post:

generate 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'

Even though I added

t.string :login,          null: false, default: ""

to the 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

解决方案

Rails achieves find_by_sth magic by inferring an attribute name in your method call, thus returning an ActiveRecordRelation.

As you have no user_name attribute in your model, Active Record will not be able to perform a search on it (just imagine it has to be translated into an SQL query).

On the other hand, I'd re-think the logic of your user_name method if I were you. Consider persisting the user_name on your database.

By the way, in the second error you mention, notice that the example you have is working with a login attribute, which you simply don't have.

这篇关于在设计中产生有意义的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆