Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 3 变轨自动销毁_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails 3 变轨自动销毁

Ruby on rails 3 变轨自动销毁,ruby-on-rails-3,activerecord,Ruby On Rails 3,Activerecord,比如说 class User < ActiveRecord::Base has_one :avatar, :dependent => :destroy end class Avatar < ActiveRecord::Base belongs_to :user end 所以我不想手动执行最后一个命令(销毁)。是否有任何选项可供自动销毁旧对象?或者很多时候,最好使用其他方法,而不是阿凡达=方法 谢谢。为什么不更新旧的头像而不是创建新的头像,如: avatar = Av

比如说

class User < ActiveRecord::Base
  has_one :avatar, :dependent => :destroy
end

class Avatar < ActiveRecord::Base
  belongs_to :user
end
所以我不想手动执行最后一个命令(销毁)。是否有任何选项可供自动销毁旧对象?或者很多时候,最好使用其他方法,而不是阿凡达=方法


谢谢。

为什么不更新旧的头像而不是创建新的头像,如:

avatar = Avatar.find_or_initialize_by_user_id(:user_id => user.id)
avatar.attributes = new_attributes
avatar.save
此外,如果您愿意,您可以在
Avatar
模型中保存
后创建

avatar = Avatar.find_or_initialize_by_user_id(:user_id => user.id)
avatar.attributes = new_attributes
avatar.save