0

我正在使用rails 3.2.13ruby 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)>'

记录状态.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

EDIT-1 * RecordStatus.rb *

class RecordStatus < ActiveRecord::Base
acts_as_enumerated

 ACTIVE   = 1
 DISABLED = 2
 DELETED  = 3

  validates_uniqueness_of :id , :name
end
4

2 回答 2

0

看起来您的种子数据中必须有重复的名称。要确定,请将您的呼叫更改create!为:

 rs = RecordStatus.new(...)
 unless rs.save
   raise "Couldn't save #{rs.inspect}: #{rs.errors.full_messages.to_sentence}"
 end
于 2013-07-20T12:20:14.367 回答
0

Rails 3 本质上不支持create/alter/destroy instances枚举。如果我们想创建任何枚举,只需Model.enumeration_model_updates_permitted = truerecord_status.rake文件中添加。REF : Enumerations_mixin

于 2013-07-22T06:04:49.377 回答