简体   繁体   English

在创建时使用来自子记录的信息更新 rails 父记录

[英]Update a rails parent record with information from the child record upon creation

this is my first post so I'm not exactly sure how to format this.这是我的第一篇文章,所以我不确定如何格式化。 Lmk if I'm doing anything wrong. Lmk 如果我做错了什么。

I'm fairly new to rails and I am making an app that basically just allows users to submit 'grimes' (which you can think of as reviews) for other users.我对 Rails 还很陌生,我正在制作一个基本上只允许用户为其他用户提交“grimes”(您可以将其视为评论)的应用程序。 The app has two basic models: users and grimes.该应用程序有两个基本模型:用户和污垢。

User Model:用户 Model:

class User < ApplicationRecord
    has_many :grimes
end

Grimes Model:格莱姆斯 Model:

class Grime < ApplicationRecord
  belongs_to :user
  validates :user, presence: true
end

each grime has a griminess attribute, which is basically the rating from 1-5 for the grime.每个 grime 都有一个 griminess 属性,基本上是 grime 的 1-5 等级。 Each user also has a griminess attribute, which I would like to be the sum of the griminess of each of a user's associated grimes.每个用户还有一个 griminess 属性,我希望它是每个用户相关 grimes 的 griminess 的总和。 ie. IE。 if user1 has two grimes, g1 with griminess 3 and g2 with griminess 2, then user1 griminess should be 5.如果 user1 有两个 grimes,g1 有 griminess 3,g2 有 griminess 2,那么 user1 griminess 应该是 5。

I could do this by iterating through each of the user's grimes when I would like to display the user's griminess, but that seems very unnecessary.当我想显示用户的污垢时,我可以通过遍历每个用户的污垢来做到这一点,但这似乎非常不必要。 Instead, I would like to just increment the user's griminess each time a new grime associated with them is added, but I can't figure out how to do that right now.相反,我只想在每次添加与用户相关的新污垢时增加用户的污垢,但我现在不知道该怎么做。

the form to create a grime looks like this:创建 grime 的表单如下所示:

<%= form_for @grime do |f| %>
    Title: <%= f.text_field :title %><br />
    Description: <%= f.text_area :description %><br />
    Griminess Level:  <%= f.select :griminess, (0..5) %>
    Select Grimer: <%= f.collection_select(:user_id, User.all, :id, :name) %><br />
    <%= f.submit %>
<% end %> 

and my grimes#create looks like this:我的 grimes#create 看起来像这样:

def create
      @grime = Grime.new(grime_params)
      
      if @grime.save
          flash[:success] = "Grime Saved Successfully"
          redirect_to @grime
      else
          flash[:error] = "Error: Could Not Save Grime!"
          render :new
      end
  end

I feel like I should be able to do something like @grime.user.griminess += @grime.griminess in the create method, but that doesn't seem to work for me.我觉得我应该能够在 create 方法中执行类似@grime.user.griminess += @grime.griminess的操作,但这似乎对我不起作用。 Can anyone recommend a better way to approach this?谁能推荐一个更好的方法来解决这个问题?

Add after_save hook in Grime which will call a method on a grime whenever that grime is saved.Grime中添加after_save钩子,该钩子会在grime被保存时调用该grime上的方法。

class Grime < ApplicationRecord
  after_save :update_user_griminess # register the callback
  belongs_to :user
  validates :user, presence: true

  #callback body
  def update_user_griminess
    user.griminess = user.grimes.sum(:griminess) # update user griminess as sum of it's all grimes' griminess 
    user.save
  end      
end

There is an alternative after_create hook in ActiveRecord which runs just once when the object is created for the first time, but I preferred after_save here to adjust for cases when a grime 's griminess is updated. ActiveRecord 中有一个替代的after_create钩子,它在第一次创建 object 时只运行一次,但我更喜欢after_save在这里调整grime的污垢更新的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM