Ruby on rails 更新在操作中创建
我有三种型号,自动,perfil和用户。每个用户都有自己的配置文件。我需要的是,当你发布一个新的汽车你可以更新你的个人资料用户,如果数据是不同的或在时间上的变化。但不用于更新操作创建中的操作。我使用嵌套属性Ruby on rails 更新在操作中创建,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,nested-forms,nested-attributes,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,Nested Forms,Nested Attributes,我有三种型号,自动,perfil和用户。每个用户都有自己的配置文件。我需要的是,当你发布一个新的汽车你可以更新你的个人资料用户,如果数据是不同的或在时间上的变化。但不用于更新操作创建中的操作。我使用嵌套属性 def create current_user.perfil||= current_user.build_perfil @perfil = current_user.perfil @perfil.update_attributes(au
def create
current_user.perfil||= current_user.build_perfil
@perfil = current_user.perfil
@perfil.update_attributes(auto_params)
@auto = @current_user.autos.new(auto_params)
respond_to do |format|
if @auto.save
format.html { redirect_to @auto, notice: 'Auto was successfully created.' }
format.json { render :show, status: :created, location: @auto }
else
format.html { render :new }
format.json { render json: @auto.errors, status: :unprocessable_entity }
end
end
我想你要问的是,每当一个自动对象被创建时,如果数据发生了一些变化,并且你不想在AutoController的Create方法中更新,那么应该更新用户配置文件 你能做的是,在你的汽车模型中
include ActiveModel::Dirty
并定义一个
after_commit :update_user_profile, on: :create
在自动模型和回调函数中
def update_user_profile
prev_changes = self.previous_changes
end
现在,使用上一次更改,您可以检查相应的更改并相应地更新用户模型。您好,您遇到的具体问题是什么?你已经试过什么了?