如何以及在哪里处理相关记录的更新过程? [英] How and where to handle the updating process of associated records?
问题描述
我在使用Ruby on Rails 4,我想通过 update_attributes的
的方法,妥善处理更新和创造相关记录过程。也就是说,我有以下几点:
I am using Ruby on Rails 4 and I would like to properly handle the updating and creating process of associated records through the update_attributes
method. That is, I have the following:
# Model
class Article < ActiveRecord::Base
has_many :categories
accepts_nested_attributes_for :categories
end
# Controller
class ArticlesController < ApplicationController
def update
@article = Article.find(params[:id])
@article.update_attributes(update_params)
...
end
private
def update_params
params.require(:article).permit(:title, ..., :categories_attributes => [:name, ...])
end
end
# Logger for the update request
Started PATCH "/articles/6"
Processing by ArticlesController#update
Parameters: {"utf8"=>"✓", "article"=>{"title"=>"Sample title", "categories_attributes"=>{"0"=>{"name"=>"Sample Category 1", "..."=>"..."}, "1"=>{"name"=>"Sample Category 2", "..."=>"..."}, : "..." => {...}}
...
这个问题是关系到Rails的方式处理事情在数据库中更新通过 @article
对象类别时,更新相关记录,在我的情况:当数据被提交所以火更新
操作和参数传递给 update_attributes的
方法(如上面的记录器),然后滑轨创建新的类别记录在数据库中,每一个元素present中的散列 categories_attributes
。
The issue is related to the way Rails handles things for updating associated records in the database, in my case when updating categories through the @article
object: when data is submitted so to fire the update
action and parameters are passed to the update_attributes
method (as shown in the logger above) then Rails creates new category records in the database, one for each element present in the hash categories_attributes
.
然而,的我的意思是,如果这些存在更新现有的类别记录或创建新的,如果这些不存在的,相应的的article_id $ C $的唯一性C>和
名称
在类别列
数据库表,probabily执行搜索数据present在这些列为了判断是否需要更新或创建新的记录。事实上,在编辑文章的观点我显示与编辑类别,包括字段输入字段的表单pre-填充与现有的类别和空字段可以创建对飞类别的数据由编辑用户。
However, my intention is to update existing category records if those exist or create new ones if those do not exist, accordingly to the uniqueness of article_id
and name
columns in the categories
database table, probabily performing a search for data present in these columns in order to check if it is needed to update or create new records. In fact, in the "edit article" view I display a form with input fields for editing categories including fields pre-populated with data related to existing categories and empty fields for categories that can be created "on the fly" by the editor user.
我如何妥善处理这一问题?难道模型或控制器的责任?或者,也许,有没有更好的方式来直接在编辑文章视图管理类别?
How can I properly handle this behavior? Is it model or controller responsability? Or, maybe, is there a better way to manage categories directly in the "edit article" view?
推荐答案
您应该添加 categories_attributes
为 attr_accessible
的你的文章
模式。像:
You should add categories_attributes
as attr_accessible
in your Article
model. Like:
attr_accessible :categories_attributes
如果没有这一行,你一定要面对像上述问题,创造新的记录,而不是更新现有的。另外有一个叫宝石 nested_form
它做了出色的工作在建设这些复杂的形式。
Without this line you are bound to face issues like above, creating new records instead of updating existing ones. Also there is a gem called nested_form
which does an excellent job in building these complex forms.
这篇关于如何以及在哪里处理相关记录的更新过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!