为什么相关模型会被触及?

时间:2011-10-27 09:16:17

标签: ruby-on-rails activerecord

我有3个型号:

class DeliveryMethod
  has_many :subscription_delivery_methods
  has_many :subscriptions, :through => :subscription_delivery_methods
end

class SubscriptionDeliveryMethod < ActiveRecord::Base
  belongs_to :subscription
  belongs_to :delivery_method
end

class Subscription < ActiveRecord::Base
  has_one :subscription_delivery_method
  has_one :delivery_method, :through => :subscription_delivery_method
end

我将发送方式分配给Subscription,如下所示:

s.delivery_method = DeliveryMethod.find 1

当我按照上面的Rails更新subscription_delivery_methods进行分配时,这是预期的:

UPDATE subscription_delivery_methods SET delivery_method_id = 1, updated_at = '2011-10-27 09:11:23' WHERE subscription_delivery_methods.id = 2

但当我s.save!时,它会触及DeliveryMethod,这是意料之外的:

UPDATE delivery_methods SET updated_at = '2011-10-27 08:40:53' WHERE delivery_methods.id = 1

我在关联上尝试了各种:readonly:touch标记,以防止发生此更新。我没有成功。你们知道怎么阻止它吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

也许autosave

 has_one :delivery_method, :through => :subscription_delivery_method, :autosave => false