Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于Rails中创建时添加的其他字段更新MySQL字段_Mysql_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

基于Rails中创建时添加的其他字段更新MySQL字段

基于Rails中创建时添加的其他字段更新MySQL字段,mysql,ruby-on-rails,ruby,ruby-on-rails-4,Mysql,Ruby On Rails,Ruby,Ruby On Rails 4,随着Facebook API停止用户名检索,我需要在Rails中通过添加以下字段来创建用户名: User.username = first_name + last_name + rand(99999) 你知道怎么做吗?找到所有你想更新的用户,并通过他们进行迭代。在这种情况下,我将更新每个用户,批量为1000。关于 对于新用户,只需在创建回调之前向用户模型中添加一个 before_create :generate_username .... def generate_username b

随着Facebook API停止用户名检索,我需要在Rails中通过添加以下字段来创建用户名:

User.username = first_name + last_name + rand(99999)

你知道怎么做吗?

找到所有你想更新的用户,并通过他们进行迭代。在这种情况下,我将更新每个用户,批量为1000。关于

对于新用户,只需在创建回调之前向用户模型中添加一个

before_create :generate_username

....

def generate_username
  begin
     self.username ="#{self.user_first_name}_#{self.last_name}_#{rand(99999)}"
  end while User.exists?(:username => self.username)
end
before_create :generate_username

....

def generate_username
  begin
     self.username ="#{self.user_first_name}_#{self.last_name}_#{rand(99999)}"
  end while User.exists?(:username => self.username)
end