2013-06-20 58 views
4

我有一個模型A有許多使用:has_many關聯的模型B的對象。當我創建模型A的新對象時,設置它的屬性,構建關聯的對象並檢查非持久對象,它可以正常工作。但是,只要我嘗試使用savesave!來堅持對象,我就會得到以下回溯。未定義的方法insert_record爲陣列

NoMethodError: undefined method `insert_record' for #<Array:0x000000052541f8> 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:154:in `send' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/autosave_association.rb:306:in `block in save_collection_association' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/associations/association_collection.rb:431:in `block in method_missing' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:216:in `block in method_missing' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:216:in `each' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:216:in `method_missing' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/associations/association_collection.rb:431:in `method_missing' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/autosave_association.rb:297:in `save_collection_association' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/autosave_association.rb:163:in `block in add_autosave_association_callbacks' 
    from /usr/lib/ruby/gems/2.0.0/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:415:in `_run_create_callbacks' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:281:in `create' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/persistence.rb:246:in `create_or_update' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:277:in `block in create_or_update' 
    from /usr/lib/ruby/gems/2.0.0/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:418:in `_run_save_callbacks' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:277:in `create_or_update' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/persistence.rb:39:in `save' 
... 2 levels... 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/transactions.rb:240:in `block (2 levels) in save' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/transactions.rb:207:in `transaction' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/transactions.rb:290:in `with_transaction_returning_status' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/transactions.rb:240:in `block in save' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/transactions.rb:251:in `rollback_active_record_state!' 
    from /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.0.5/lib/active_record/transactions.rb:239:in `save' 
    from (irb):104 
    from /usr/lib/ruby/gems/2.0.0/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start' 
    from /usr/lib/ruby/gems/2.0.0/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start' 
    from /usr/lib/ruby/gems/2.0.0/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>' 
    from /home/amitav/WMS/trunk/script/rails:6:in `require' 
    from /home/amitav/WMS/trunk/script/rails:6:in `<top (required)>' 
    from -e:1:in `load' 
    from -e:1:in `<main>' 

我使用ruby 2.0.0p195和rails 3.0.5。

+0

查看我的回答。這是2小時前,但你沒有提到我的回答是對還是錯。 –

回答

2

這可能是一個兼容性問題。將您的Rails升級到更新的版本v3.2 >=

+2

正如https://github.com/rails/rails/issues/11026所驗證的那樣,該bug在當前支持的版本中不存在。 – dknight

0

另外,如果您不想升級您的Rails 3.0.x項目,請確保您運行的是開發它的Ruby版本,例如, 1.9.2或1.9.3。 與rvm或rbenv應該不是一個問題。

相關問題