2011-10-27 56 views
3

我有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 

我给你传递方法将预订这样的:

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标志,以防止此更新的发生。我没有成功。你们知道如何阻止它吗?

非常感谢。

+0

我觉得你错在你们的关系分配: 尝试写:DeliveryMethod属于许多...... ,如果它的作品告诉我。 – pimpin

回答

0

也许autosave

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

没有工作。 1)has_one没有':autosave'参数。 2)我试着':自动保存'与'belongs_to' - 它没有任何影响:( – arkadiy