我有一个用户模型,其中有一个 图片。我怎样才能禁用自动保存选项。当我尝试以下操作时出现错误。 我正在使用 Rails 3。
class User < ActiveRecord::Base
has_one :image_mapping, :class_name=>"ImageMapping",:as => :imageable
has_one :image, :through => :image_mapping, :class_name => "Image", :autosave => false
end
它说未知 key :自动保存
没有正确加载 autosave_association.rb 吗?
最佳答案
这可能无法解决您的问题,但也许您发现它有帮助:
has_one :through
似乎有问题。例如在 class Post
中,
has_one :owner, :through => :blog
工作正常,但是
has_one :owner, :through => :blog, :autosave => false
没有。如果你实例化一个Post
,像这样
post = Post.find 1
你明白了:
ArgumentError: Unknown key(s): autosave
from /var/lib/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys'
from /var/lib/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/associations.rb:1758:in `create_has_one_through_reflection'
from /var/lib/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/associations.rb:1102:in `has_one'
from /var/lib/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/autosave_association.rb:137:in `has_one'
[...]
查看the documentation ,我找不到它不应该工作的原因。事实上,它似乎可以与 has_many
一起正常工作,如果设置了 :autosave
选项,只有 has_one
会出现问题。
除非 Stack Overflow 上有人发现以这种方式使用 :has_one
有问题,否则您也可以转到 Rails mailing list求助;我认为 Rails-core 是解决这个问题的正确选择。
关于ruby-on-rails - rails 3 : How to disable autosave of has_one and has_many associations?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4400204/