我在CentOS 5.6
中使用rails 3.2.13
和ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-linux]
。当我尝试对数据进行种子处理时,我出现了以下错误。
** 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
在种子数据中似乎必须有重复的名称。要确定答案,请将您对create!
的呼叫更改为类似的呼叫
rs = RecordStatus.new(...)
unless rs.save
raise "Couldn't save #{rs.inspect}: #{rs.errors.full_messages.to_sentence}"
end
Rails 3本质上不支持create/alter/destroy instances
进行枚举。如果我们想创建任何枚举,只需在record_status.rake
文件中添加Model.enumeration_model_updates_permitted = true
即可。REF:Enumerations_mixin