Rails:当记录不存在时记录存在回滚
Rails: Record Exists Rollback when Record doesn't exist
我是 rails 的新手。我正在尝试创建一个用户并在检查唯一性时获得回滚。我专门在开发数据库中工作。我可以在我的索引页上看到查询的用户,所以数据库确实匹配。
Started POST "/users" for 127.0.0.1 at 2015-01-01 17:02:17 -0600
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"<TOKEN THAT MATCHES THE FORM>", "user"=>{"first_name"=>"First", "last_name"=>"Test", "username"=>"test1", "email"=>"test1@self.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create User"}
(0.2ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`username` = BINARY 'test1' LIMIT 1
User Exists (0.2ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'test1@self.com' LIMIT 1
(0.1ms) ROLLBACK
Rendered users/_form.html.erb (1.9ms)
Rendered users/new.html.erb within layouts/application (3.8ms)
Completed 200 OK in 178ms (Views: 56.3ms | ActiveRecord: 0.9ms)
所以我检查了我的 table 看它是否确实存在。
mysql> SELECT * FROM users;
+----+------------+---------------+------------------+------------+--------------------+---------------------+---------------------+
| id | first_name | last_name | email | username | password_digest | created_at | updated_at |
+----+------------+---------------+------------------+------------+--------------------+---------------------+---------------------+
| 1 | Initial | User | test0@self.com | test0 | <SOME LONG DIGEST> | 2015-01-01 03:56:39 | 2015-01-01 03:56:39 |
+----+------------+---------------+------------------+------------+--------------------+---------------------+---------------------+
1 row in set (0.00 sec)
如果我像 Rails 那样运行命令:
mysql> SELECT 1 AS one FROM `users` WHERE `users`.`username` = BINARY 'test1' LIMIT 1;
Empty set (0.00 sec)
mysql> SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'test1@self.com' LIMIT 1;
Empty set (0.00 sec)
没有。为什么会发生这种情况,是否有一种好方法可以查看实际发生的情况?
我猜这很可能是以下任一方面的问题:
A) 模型中的验证(全部删除并再次测试)- 如果有效,一次添加一次,然后找出导致回滚的验证。
例子models/user.rb
validates :username, presence: true, uniqueness: { case_sensitive: false }
B) 您错过了将要保存的属性之一添加到参数列表的机会
示例 controllers/users_controller.rb:
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :username, :password_digest, :_destroy)
end
试试 (A) - 我遇到过很多次。
您可以尝试评论您的验证(在您的模型代码中)并使用您的种子而不会出现验证问题。
但不推荐这样做,因为您将成为未经验证的 inserting/manipulation 数据。
示例:
class Client < ActiveRecord::Base
#has_many :contracts
#validates_cpf_or_cnpj :cpf_or_cnpj
#validates_uniqueness_of :cpf_or_cnpj, message: "exists."
#validates_uniqueness_of :rg, message: "exists."
我是 rails 的新手。我正在尝试创建一个用户并在检查唯一性时获得回滚。我专门在开发数据库中工作。我可以在我的索引页上看到查询的用户,所以数据库确实匹配。
Started POST "/users" for 127.0.0.1 at 2015-01-01 17:02:17 -0600
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"<TOKEN THAT MATCHES THE FORM>", "user"=>{"first_name"=>"First", "last_name"=>"Test", "username"=>"test1", "email"=>"test1@self.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create User"}
(0.2ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`username` = BINARY 'test1' LIMIT 1
User Exists (0.2ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'test1@self.com' LIMIT 1
(0.1ms) ROLLBACK
Rendered users/_form.html.erb (1.9ms)
Rendered users/new.html.erb within layouts/application (3.8ms)
Completed 200 OK in 178ms (Views: 56.3ms | ActiveRecord: 0.9ms)
所以我检查了我的 table 看它是否确实存在。
mysql> SELECT * FROM users;
+----+------------+---------------+------------------+------------+--------------------+---------------------+---------------------+
| id | first_name | last_name | email | username | password_digest | created_at | updated_at |
+----+------------+---------------+------------------+------------+--------------------+---------------------+---------------------+
| 1 | Initial | User | test0@self.com | test0 | <SOME LONG DIGEST> | 2015-01-01 03:56:39 | 2015-01-01 03:56:39 |
+----+------------+---------------+------------------+------------+--------------------+---------------------+---------------------+
1 row in set (0.00 sec)
如果我像 Rails 那样运行命令:
mysql> SELECT 1 AS one FROM `users` WHERE `users`.`username` = BINARY 'test1' LIMIT 1;
Empty set (0.00 sec)
mysql> SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'test1@self.com' LIMIT 1;
Empty set (0.00 sec)
没有。为什么会发生这种情况,是否有一种好方法可以查看实际发生的情况?
我猜这很可能是以下任一方面的问题:
A) 模型中的验证(全部删除并再次测试)- 如果有效,一次添加一次,然后找出导致回滚的验证。
例子models/user.rb
validates :username, presence: true, uniqueness: { case_sensitive: false }
B) 您错过了将要保存的属性之一添加到参数列表的机会
示例 controllers/users_controller.rb:
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :username, :password_digest, :_destroy)
end
试试 (A) - 我遇到过很多次。
您可以尝试评论您的验证(在您的模型代码中)并使用您的种子而不会出现验证问题。
但不推荐这样做,因为您将成为未经验证的 inserting/manipulation 数据。
示例:
class Client < ActiveRecord::Base
#has_many :contracts
#validates_cpf_or_cnpj :cpf_or_cnpj
#validates_uniqueness_of :cpf_or_cnpj, message: "exists."
#validates_uniqueness_of :rg, message: "exists."