【发布时间】:2013-01-30 01:51:12
【问题描述】:
我很难让 updated_at 列更新通过 CasinoAmenity 拥有_many 便利设施的赌场模型。赌场的 updated_at 列在添加设施时会正确更新,但在移除时不会。
这是我的(简化的)模型:
赌场.rb
class Casino < ActiveRecord::Base
has_many :amenity_casino, :dependent => :destroy
has_many :amenities, :through => :amenity_casino
end
amenity_casino.rb
class AmenityCasino < ActiveRecord::Base
belongs_to :casino, :touch => true
belongs_to :amenity, :touch => true
end
amenity.rb
class Amenity < ActiveRecord::Base
has_many :amenity_casinos, :dependent => :destroy
has_many :casinos, :through => :amenity_casinos
end
现在到控制台。检查初始 updated_at 时间。
> Casino.find(5).updated_at
=> Wed, 30 Jan 2013 00:30:04 UTC +00:00
为赌场增添便利设施。
> Casino.find(5).amenities << Amenity.first
=> [#<Amenity id: 1, name: "asdf", weight: "", created_at: "2012-11-10 02:29:14", updated_at: "2013-01-30 01:29:14", description: "asdf">]
确认关联已保存到数据库中
> Casino.find(5).amenities
=> [#<Amenity id: 1, name: "asdf", weight: "", created_at: "2012-11-10 02:29:14", updated_at: "2013-01-30 01:29:14", description: "asdf">]
我们可以看到该赌场的 updated_at 时间已按预期更改。
> Casino.find(5).updated_at
=> Wed, 30 Jan 2013 01:29:14 UTC +00:00
现在让我们从赌场中移除该设施。
> Casino.find(5).amenities = []
=> []
现在确保它可以正常访问数据库
> Casino.find(5).amenities
=> []
现在让我们检查一下赌场的 updated_at 列...
> Casino.find(5).updated_at
=> Wed, 30 Jan 2013 01:29:14 UTC +00:00
如您所见,这与我们从赌场移除设施之前的情况相同。我还尝试在 AmenityCasino 上使用 before_destroy 过滤器来触摸赌场,但这似乎没有奏效。
我很想听听这里是否有人有任何解决方案。如果这很重要,我将使用 ActiveAdmin 来管理所有内容。
Rails 版本是 3.2.11
【问题讨论】:
-
这确实很奇怪。
标签: ruby-on-rails ruby-on-rails-3.2 rails-activerecord