gpt4 book ai didi

ruby-on-rails - 保存父项时,Embeds_many 子属性不会保留

转载 作者:可可西里 更新时间:2023-11-01 10:45:16 25 4
gpt4 key购买 nike

我已经找了好几天都没有找到我的问题的确切答案,这个答案就这么简单:我有一个简单的模型,有书和作者。一本书嵌入了许多作者,而作者嵌入在书中。但是每当我保存一本新书时,作者数组就不会保留。

我有一个 Angular 7 应用程序,调用 ROR API。我的 Rails 版本是 5.2.2。我正在使用 mongoid 7.0 进行持久化。

我的 API 是使用 rails g scaffold 以及 --api 和 --skip-active-record 标志生成的。

我的属性映射首先遇到了问题。当 Rails 等待形成 lower_snake_case 变量时,我的 Angular 应用程序以 lowerCamelCase 发送 JSON。我设法通过在我的初始化器中添加一个中间件(如果我在这个上错了请纠正我)来绕过这个问题,它将 camelCase 转换为 snake_case。

# Transform JSON request param keys from JSON-conventional camelCase to
# Rails-conventional snake_case:
ActionDispatch::Request.parameter_parsers[:json] = -> (raw_post) {
# Modified from action_dispatch/http/parameters.rb
data = ActiveSupport::JSON.decode(raw_post)
data = {:_json => data} unless data.is_a?(Hash)

# Transform camelCase param keys to snake_case:
data.deep_transform_keys!(&:underscore)
}

根据我发现的问题,这可能是强参数的问题,所以我试图在我的 book_params 中解决这个问题

def book_params
#params.fetch(:book, {})
params.require(:book).permit(:title, :release_date, authors_attributes: [:name, :last_name, :birth_date])
end

这些是我的模型:

class Person
include Mongoid::Document
field :last_name, type: String
field :first_name, type: String
field :birth_date, type: Date
end

class Author < Person
include Mongoid::Document
embedded_in :book
end

class Book
include Mongoid::Document
field :title, type: String
field :release_date, type: Date
embeds_many :authors
accepts_nested_attributes_for :authors
end

这是我的图书 Controller 中的 POST(使用 Rails 生成)

  # POST /books
def create

@book = Book.new(book_params)

if @book.save
render json: @book, status: :created, location: @book
else
render json: @book.errors, status: :unprocessable_entity
end
end

下面是发送、接收的主体以及 Rails 如何处理它的示例:

Request sent by angular app

Request received and processed by Rails

我们可以在book对象中看到

"book"=>{"title"=>"azerty", "release_date"=>"2019-01-21T16:10:19.515Z"}}

作者已经消失,尽管他们出现在服务器收到的请求中。

我的问题是:解决这个问题的方法是什么,或者至少我缺少什么?使用嵌入式文档和 accepts_nested_attributes_for 时,Mongoid 不会自动保存 child 吗?每次在我的 Controller 中保存父项时,我是否应该手动保存子项?

在此先感谢您对我的帮助

最佳答案

您必须使用嵌套属性来保存子记录

book 模型中添加以下行

accepts_nested_attributes_for :authors

并在 author_attributes 中传递 authors 参数,例如:

{title: 'test', release_date: '', author_attributes: [{first_name: '', other_attributes of author}, {first_name: '', , other_attributes of author}]}

更多详情请查看Mongoid: Nested attributes

以这种格式传递参数

{"title"=>"test", "release_date"=>"2019-01-22", "book"=>{"title"=>"test", "release_date"=>"2019-01-22", "authors_attributes"=>[{"first_name"=>"test name", "last_name"=>"test", "birth_date"=>"2019-01-22T09:43:39.698Z"}]}}

允许书籍参数

def book_params
params.require(:book).premit(:first_name, :last_name, authors_attributes: %i[first_name last_name birth_date])
end

关于ruby-on-rails - 保存父项时,Embeds_many 子属性不会保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54304925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com