ActiveRecord :: RecordNotSaved - Rails 3播种数据

时间:2013-07-20 07:27:35

标签: ruby-on-rails ruby ruby-on-rails-3.2 ruby-1.9.2

我在rails 3.2.13中使用ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-linux]CentOS 5.6。当我尝试播种数据时,我收到了以下错误。

** Execute Samp:seed:record_status
Creating RecordStatus: Active
rake aborted!
ActiveRecord::RecordNotSaved
/usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.13/lib/active_record/persistence.rb:105:in `save!'
/usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.13/lib/active_record/validations.rb:56:in `save!'
/usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.13/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
/usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.13/lib/active_record/transactions.rb:264:in `block in save!'
/opt/Samp/lib/tasks/seed/record_status.rake:19:in `block (4 levels) in <top (required)>'
/opt/Samp/lib/tasks/seed/record_status.rake:16:in `each'
/opt/Samp/lib/tasks/seed/record_status.rake:16:in `block (3 levels) in <top (required)>'

record_status.rake

16:  record_statuses.each do |rs|
17:       unless RecordStatus.find_by_seed_name(rs[:name])
18:       puts "Creating RecordStatus: #{rs[:name]}"
19:       RecordStatus.create!(:name        => rs[:name],
20:                             :seed_name   => rs[:name],
21:                             :description => rs[:description])
22:        end
23:     end

修改-1 *的 RecordStatus.rb *

class RecordStatus < ActiveRecord::Base
acts_as_enumerated

 ACTIVE   = 1
 DISABLED = 2
 DELETED  = 3

  validates_uniqueness_of :id , :name
end

2 个答案:

答案 0 :(得分:0)

您的种子数据中似乎必须有重复的名称。要确定,请将您的号码更改为create!,例如:

 rs = RecordStatus.new(...)
 unless rs.save
   raise "Couldn't save #{rs.inspect}: #{rs.errors.full_messages.to_sentence}"
 end

答案 1 :(得分:0)

Rails 3本质上不支持create/alter/destroy instances枚举。如果我们要创建任何枚举,只需在Model.enumeration_model_updates_permitted = true文件中添加record_status.rake即可。参考:Enumerations_mixin