我对 Rails 协会中的触摸指令有严重问题。
它总是编辑 updated_at 时间戳。这很奇怪,因为当关联对象没有改变时,总是更新时间戳。
有地方
class Place < ActiveRecord::Base
has_one :location, inverse_of: :place, dependent: :destroy
attr_accessible :name
attr_accessible :location_attributes
accepts_nested_attributes_for :location
end
和位置
class Location < ActiveRecord::Base
belongs_to :place, inverse_of: :location, touch: true
attr_accessible :number, :street, :locality, :region, :postal_code, :country, :description, :description_en, :latitude, :longitude
attr_accessible :place_id
validates :place, presence: true
validates :number, presence: true
validates :street, presence: true
validates :locality, presence: true
validates :postal_code, presence: true, postal_code: { country: :sk }
validates :latitude, presence: true, numericality: true, allow_nil: true
validates :longitude, presence: true, numericality: true, allow_nil: true
end
当我加载并保存存在的地方而不进行更改时:
Place.last.save # changed? == false
它会自动编辑 updated_at
SQL (0.4ms) UPDATE `places` SET `updated_at` = '2012-11-18 20:38:48' WHERE `places`.`id` = 490
有人对此有任何经验吗?如何解决?
使用 Rails 3.2.9 和 MySQL(mysql2 适配器)
编辑:
Location.last.save 更改为 Place.last.save。这是错误的。