【问题标题】:Why is the associated model being touched?为什么要触摸关联的模型?
【发布时间】:2011-10-27 09:16:17
【问题描述】:

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

非常感谢。

【问题讨论】:

  • 我认为您的关系分配错误:尝试写:DeliveryMethod 属于许多...并告诉我它是否有效。

标签: ruby-on-rails activerecord


【解决方案1】:

也许autosave

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

【讨论】:

  • 没用。 1) has_one 没有:autosave 参数。 2) 我用belongs_to 尝试了:autosave - 它没有任何效果:(
猜你喜欢
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多