Rails 3.1.2:设置关联而不保存

时间:2014-05-16 03:56:33

标签: ruby-on-rails ruby-on-rails-3

我想在一个activerecord对象上设置一个关联而不保持该关联,因此当我调用关联方法时,我将返回我刚刚设置的关联。有没有一种很好的方法呢?

示例:

class Bar < ActiveRecord::Base
   has_many :foos
end

class Foo < ActiveRecord::Base
   belongs_to :bar
end

this_bar = Bar.find(1)
this_foo = Foo.find(1)

this_bar.foos = [this_foo] # This performs a save, which I don't want
this_bar.foos #=> should return [this_foo]
# further computation #

(我想要实现的目标是允许用户查看&#34;预览&#34;更新后对象的外观,但仍然可以选择取消更新。)

0 个答案:

没有答案