3
我有一些小测试,为Ruby on Rails的3.2.1这里是我的模型解释:的ActiveRecord :: AssociationTypeMismatch在Ruby on Rails的3.2.1
class Project < ActiveRecord::Base
has_many :tasks
accepts_nested_attributes_for :tasks
end
class Task < ActiveRecord::Base
belongs_to :project
end
但是,当我试图挽救任务为我的项目我得到错误:
irb(main):037:0> Project.first.tasks = [Task.first.id, Task.last.id]
Project Load (0.2ms) SELECT "projects".* FROM "projects" LIMIT 1
Task Load (0.1ms) SELECT "tasks".* FROM "tasks" LIMIT 1
Task Load (0.1ms) SELECT "tasks".* FROM "tasks" ORDER BY "tasks"."id" DESC LIMIT 1
ActiveRecord::AssociationTypeMismatch: Task(#70101178643360) expected, got Fixnum(#70101143633040)
from /Users/ka8725/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'
from /Users/ka8725/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:308:in `block in replace'
from /Users/ka8725/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:308:in `each'
from /Users/ka8725/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:308:in `replace'
from /Users/ka8725/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:41:in `writer'
from /Users/ka8725/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/associations/builder/association.rb:51:in `block in define_writers'
from (irb):37
from /Users/ka8725/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /Users/ka8725/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /Users/ka8725/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
请帮助我。为什么会引发这个异常?
糟糕!我的错。在我看来,我今天过度劳累了...... – ka8725 2012-03-15 15:22:45