ActiveRecord在保存和创建时从插入语句中删除电子邮件字段

时间:2015-06-25 03:31:34

标签: ruby-on-rails activerecord sqlite

我遇到了刚出现的问题。当我尝试创建用户时,ActiveRecord生成的sql会从insert语句中删除电子邮件字段,从而导致验证错误,因为需要发送电子邮件。

你们中的任何人都知道会发生什么吗?

User.create(email: "haiyun211@gmail.com", firstname: "Jonathan", lastname: "Jensen", 
password: "testing123", password_confirmation: "testing123")
(0.1ms)  begin transaction
User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('haiyun211@gmail.com') LIMIT 1
SQL (0.7ms)  INSERT INTO "users" ("firstname", "lastname", "password_digest", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["firstname", "Jonathan"], ["lastname", "Jensen"], 
["password_digest", "$2a$10$gmnwqEWUb2kk3MeObzbcLO1wN4ZBM1jmLlk5uS0zMPzufe1OjPcp2"], ["created_at", "2015-06-25 03:29:25.575779"], ["updated_at", "2015-06-25 03:29:25.575779"]]
SQLite3::ConstraintException: NOT NULL constraint failed: users.email: INSERT INTO "users" ("firstname", "lastname", "password_digest", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
(0.1ms)  rollback transaction
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed: users.email: INSERT INTO "users" ("firstname", "lastname", "password_digest", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:108:in `step'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:108:in `block in each'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:107:in `loop'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:107:in `each'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:314:in `to_a'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:314:in `block in exec_query'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:293:in `exec_query'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:76:in `exec_insert'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:108:in `insert'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/relation.rb:64:in `insert'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/persistence.rb:522:in `_create_record'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activerecord-4.2.1/lib/active_record/counter_cache.rb:139:in `_create_record'
... 34 levels...
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/jjensen/Projects/Rails/timesheet/bin/rails:8:in `<top (required)>'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/jjensen/.rvm/gems/ruby-2.1.2@blog/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/jjensen/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/jjensen/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'2.1.2 :003 > 

1 个答案:

答案 0 :(得分:1)

我的用户模型中存在问题。它删除电子邮件的原因是因为这行

before_save {self.email = email.downcase!}

删除!修好了。

before_save {self.email = email.downcase}