在一个帖子中创建父类和子对象的最佳方法是什么?

时间:2021-07-13 22:59:46

I have two applications, App1 and App2. App1 posts a JSON payload to App2 that includes data for a parent and child object. If the parent object already exists in App2, then we update the parent record if anything has changed and create the child record in App2. If the parent object does not exist in App2, we need to first create it, then create the child object and associate the two. Right now I'm doing it like this:

我有两个应用程序,App1和App2。App1向App2发布一个JSON有效负载,其中包含父对象和子对象的数据。如果父对象已经存在于App2中,那么如果有任何更改,我们将更新父记录,并在App2中创建子记录。如果父对象在App2中不存在,我们需要首先创建它,然后创建子对象并将两者关联起来。现在我是这样做的:

class ChildController
  def create
    @child = Child.find_or_initialize_by_some_id(params[:child][:some_id])
    @child.parent = Parent.create_or_update(params[:parent])

    if @child.update_attributes(params[:child])
      do_something
    else
      render :json => @child.errors, :status => 500
    end
  end
end

Something feels dirty about creating/updating the parent like that. Is there a better way to go about this? Thanks!

创建/更新父类时感觉有些脏。有更好的办法吗?谢谢!

2 个解决方案

#1


3  

As a starting point, you'll want to create the association in your model, then include accepts_nested_attributes_for on your Parent.

作为起点,您将希望在模型中创建关联,然后将accepts_nested_attributes_for包含在父模型中。

With the associations created in your model, you should be able to manipulate the relationship pretty easily, because you automatically get a host of methods intended to manage the relationship. For example, your Parent/Child model might look something like this:

使用在模型中创建的关联,您应该能够相当容易地操纵关系,因为您将自动获得许多用于管理关系的方法。例如,您的父/子模型可能是这样的:

In your Parent model:

在你的父母模型:

class Parent < ActiveRecord::Base
  has_many :children
  accepts_nested_attributes_for :children

In your Child model:

在你的孩子模型:

class Child < ActiveRecord::Base
  belongs_to :parent

Then, you should be able to build associations in your controller like this:

然后,您应该能够在您的控制器中建立这样的关联:

def new
    @parent = Parent.children.build
end

def create
   @parent = Parent.children.build(params[:parent])
end

The nested_attributes property will then allow you to update attributes of the Child by manipulating the Parent.

然后,nested_attributes属性将允许您通过操作父元素更新子元素的属性。

Here is the Rails API on the topic: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

以下是关于这个主题的Rails API: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

#2


0  

Use accept_nested_attributes_for to handle parent children relationship .here's a blog post to help you out http://currentricity.wordpress.com/2011/09/04/the-definitive-guide-to-accepts_nested_attributes_for-a-model-in-rails-3/

使用accept_nested_attributes_for来处理父子关系。这里有一篇博客文章可以帮助您了解http://currentricity.wordpress.com/2011/09/04/definits_nested_attributes_for -a- models -in- rails3/

#1


3  

As a starting point, you'll want to create the association in your model, then include accepts_nested_attributes_for on your Parent.

作为起点,您将希望在模型中创建关联,然后将accepts_nested_attributes_for包含在父模型中。

With the associations created in your model, you should be able to manipulate the relationship pretty easily, because you automatically get a host of methods intended to manage the relationship. For example, your Parent/Child model might look something like this:

使用在模型中创建的关联,您应该能够相当容易地操纵关系,因为您将自动获得许多用于管理关系的方法。例如,您的父/子模型可能是这样的:

In your Parent model:

在你的父母模型:

class Parent < ActiveRecord::Base
  has_many :children
  accepts_nested_attributes_for :children

In your Child model:

在你的孩子模型:

class Child < ActiveRecord::Base
  belongs_to :parent

Then, you should be able to build associations in your controller like this:

然后,您应该能够在您的控制器中建立这样的关联:

def new
    @parent = Parent.children.build
end

def create
   @parent = Parent.children.build(params[:parent])
end

The nested_attributes property will then allow you to update attributes of the Child by manipulating the Parent.

然后,nested_attributes属性将允许您通过操作父元素更新子元素的属性。

Here is the Rails API on the topic: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

以下是关于这个主题的Rails API: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

#2


0  

Use accept_nested_attributes_for to handle parent children relationship .here's a blog post to help you out http://currentricity.wordpress.com/2011/09/04/the-definitive-guide-to-accepts_nested_attributes_for-a-model-in-rails-3/

使用accept_nested_attributes_for来处理父子关系。这里有一篇博客文章可以帮助您了解http://currentricity.wordpress.com/2011/09/04/definits_nested_attributes_for -a- models -in- rails3/