Ruby on rails 从记录中删除/添加关联不会更新时间戳列
给定以下控制器代码Ruby on rails 从记录中删除/添加关联不会更新时间戳列,ruby-on-rails,Ruby On Rails,给定以下控制器代码 def update params[:image][:contributor_ids] ||= [] params[:image][:category_ids] ||= [] @image = Image.find( params[:id] ) if @image.update_attributes params[:image] redirect_to session[:return_to] else render :edit end
def update
params[:image][:contributor_ids] ||= []
params[:image][:category_ids] ||= []
@image = Image.find( params[:id] )
if @image.update_attributes params[:image]
redirect_to session[:return_to]
else
render :edit
end
end
和下面的图像模型
class Image < ActiveRecord::Base
has_many :category_links, :as => :categorizable, :dependent => :destroy
has_many :categories, :through => :category_links
has_many :contributions, :as => :contributable, :dependent => :destroy
has_many :contributors, :through => :contributions
# .... omitted for clarity
end
类映像:categorizable,:dependent=>:destroy
有多个:类别,:通过=>:类别链接
有多个:贡献,:as=>:可贡献,:依赖=>:销毁
有许多:贡献者,:至=>:贡献者
# .... 为清楚起见省略
结束
调用update\u属性时,仅更改关联
ActiveRecord不会更改
列中的updated\u-为什么会这样以及如何更改该行为?我认为这是因为当您有一个N:N关系并更新该关系时,实际上是在创建/修改/删除联接表中的记录(在这种情况下,贡献
或类别链接
),而不是实际更新@image
实例本身