2013-07-20 29 views
0

我正在使用CentOS 5.6rails 3.2.13ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-linux]。當我嘗試播種數據時,出現以下錯誤。的ActiveRecord :: RecordNotSaved - Rails 3個的播種數據

** 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 

EDIT-1 * RecordStatus.rb *

class RecordStatus < ActiveRecord::Base 
acts_as_enumerated 

ACTIVE = 1 
DISABLED = 2 
DELETED = 3 

    validates_uniqueness_of :id , :name 
end 
+0

你有什麼樣的模型驗證對RECORDSTATUS模型? – apotry

+0

@ peri08我更新了我的問題 – Bathakarai

回答

0

的Rails 3不支持create/alter/destroy instances由自然枚舉。如果我們想創建的任何枚舉只是record_status.rake文件中添加Model.enumeration_model_updates_permitted = true。 REF:Enumerations_mixin

0

它看起來像你必須有重複您的種子數據中的名稱。爲了找到肯定,你的電話create!更改爲類似:

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