Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从记录中删除/添加关联不会更新时间戳列_Ruby On Rails - Fatal编程技术网

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
实例本身