我无法弄清楚我的用户模型上的这个创建语句出了什么问题:
User.create({first_name: "Alan",....}, :without_protection => true)
给我堆栈跟踪:
ArgumentError: wrong number of arguments (2 for 1)
from /Users/alanheppenstall/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-4.0.0/lib/active_record/persistence.rb:32:in `create'
from (irb):56
from /Users/alanheppenstall/.rvm/gems/ruby-1.9.3-p362/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/alanheppenstall/.rvm/gems/ruby-1.9.3-p362/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/alanheppenstall/.rvm/gems/ruby-1.9.3-p362/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
我的语法匹配:http://apidock.com/rails/ActiveRecord/Base/create/class/
我正在使用这种模式设计 - 这可能是个问题吗?我找不到覆盖initialize()的任何地方。
谢谢!
答案 0 :(得分:5)
好的,他们删除了Rails 4中的选项,并且在APIdock(http://apidock.com/rails/ActiveRecord/Base/create/class/)等地方尚未更新
你可以看到当前和3.2之间的区别:
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb
和
https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/persistence.rb
答案 1 :(得分:0)
它已在rails 4中删除
现在你可以在不设置标志的情况下创建
User.create({id: 101, first_name: "Alan",....})
Rails将使用 id = 101
创建新记录而不使用标记
但是,如果ID 101
的任何记录已经存在,您将收到错误
ActiveRecord :: RecordNotUnique:PG :: UniqueViolation:错误:重复键值违反唯一约束“users_pkey”