gpt4 book ai didi

ruby-on-rails-5 - rails 5 accepts_nested_attributes 不工作

转载 作者:行者123 更新时间:2023-12-02 03:13:43 25 4
gpt4 key购买 nike

我只是在用 Rails 5 做实验,然后发现了 accepts_nested_attributes_for .但我无法让它工作。 我做错了什么 ?

这是我的模型:

  • 事件

  • 这是 SQL 表定义

    型号代码
    class Person < ApplicationRecord
    has_many :activities
    accepts_nested_attributes_for :activities
    end


    class Activity < ApplicationRecord
    belongs_to :person
    end

    阅读 API 文档 ( accepts_nested_attributes_for )

    我创建了一个 params 哈希,并尝试将其提交给 Person 类。
    params = {person: {name: 'Joe', activities_attributes: [{description: "Hiking"}]} }

    当我尝试运行此特定代码时,没有创建名为“Joe”的新人员记录和“Hiking”事件下的新关联记录,但它很简单失败。
    >> params = {person: {name: 'Joe', activities_attributes: [{description: "Hiking"}]} }
    {:person=>{:name=>"Joe", :activities_attributes=>[{:description=>"Hiking"}]}}


    >> Person.create(params[:person])
    (12.6ms) BEGIN
    (0.6ms) ROLLBACK
    #<Person id: nil, name: "Joe", age: nil, adult: nil, created_at: nil, updated_at: nil>

    但是一步一步地做似乎工作得很好:
    >> Person.create(:name => "Joe")
    (6.3ms) BEGIN
    SQL (31.6ms) INSERT INTO "people" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["name", "Joe"], ["created_at", 2016-07-13 19:30:35 UTC], ["updated_at", 2016-07-13 19:30:35 UTC]]



    >> a = Person.first
    Person Load (9.1ms) SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT $1 [["LIMIT", 1]]
    #<Person id: 35, name: "Joe", age: nil, adult: nil, created_at: "2016-07-13 19:30:35", updated_at: "2016-07-13 19:30:35">


    >> a.activities_attributes = [{:name => "Hiking"}]
    [{:name=>"Hiking"}]


    >> a.save
    (0.2ms) BEGIN
    Person Load (0.6ms) SELECT "people".* FROM "people" WHERE "people"."id" = $1 LIMIT $2 [["id", 35], ["LIMIT", 1]]
    SQL (13.0ms) INSERT INTO "activities" ("person_id", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["person_id", 35], ["name", "Hiking"], ["created_at", 2016-07-13 19:31:02 UTC], ["updated_at", 2016-07-13 19:31:02 UTC]]
    (6.8ms) COMMIT
    true


    >> a
    #<Person id: 35, name: "Joe", age: nil, adult: nil, created_at: "2016-07-13 19:30:35", updated_at: "2016-07-13 19:30:35">


    >> a.activities
    Activity Load (12.4ms) SELECT "activities".* FROM "activities" WHERE "activities"."person_id" = $1 [["person_id", 35]]
    #<ActiveRecord::Associations::CollectionProxy [#<Activity id: 7, person_id: 35, name: "Hiking", created_at: "2016-07-13 19:31:02", updated_at: "2016-07-13 19:31:02", description: nil>]>

    人表
    testing_development=# \d+ people;
    Table "public.people"
    Column | Type | Modifiers | Storage | Stats target | Description
    ------------+-----------------------------+-----------------------------------------------------+----------+--------------+-------------
    id | integer | not null default nextval('people_id_seq'::regclass) | plain | |
    name | character varying | | extended | |
    age | integer | | plain | |
    adult | boolean | | plain | |
    created_at | timestamp without time zone | not null | plain | |
    updated_at | timestamp without time zone | not null | plain | |
    Indexes:
    "people_pkey" PRIMARY KEY, btree (id)

    事件表
    testing_development=# \d+ activities;
    Table "public.activities"
    Column | Type | Modifiers | Storage | Stats target | Description
    -------------+-----------------------------+---------------------------------------------------------+----------+--------------+-------------
    id | integer | not null default nextval('activities_id_seq'::regclass) | plain | |
    person_id | integer | | plain | |
    name | text | | extended | |
    created_at | timestamp without time zone | not null | plain | |
    updated_at | timestamp without time zone | not null | plain | |
    description | text | | extended | |
    Indexes:
    "activities_pkey" PRIMARY KEY, btree (id)
    "index_activities_on_person_id" btree (person_id)

    最佳答案

    我让它工作了,但不确定这是否是 Rails 5 的错误,或者只是工作方式的改变。

    我如何让它工作是看这个问题是否与 rails 5 隔离。我创建了一个 Rails 4.2 应用程序,使用完全相同的代码,和 成功了 .

    一旦我让它工作,然后我查看了在线帖子,看看我是否能找到为什么会在 Rails 中发生这种情况。这些是帮助我的资源:

    Trouble with accepts_nested_attributes_for in Rails 5.0.0.beta3, -api option

    class Post < ApplicationRecord
    belongs_to :member, optional: true
    end

    我添加了那条线,它就像一个魅力。
    >> params = {person: {name: "Testing", activities_attributes: [{name: "Testing"}]}}
    {:person=>{:name=>"Testing", :activities_attributes=>[{:name=>"Testing"}]}}


    >> Person.create(params[:person])
    (0.1ms) BEGIN
    SQL (6.5ms) INSERT INTO "people" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["name", "Testing"], ["created_at", 2016-07-13 21:30:45 UTC], ["updated_at", 2016-07-13 21:30:45 UTC]]
    SQL (12.5ms) INSERT INTO "activities" ("person_id", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["person_id", 37], ["name", "Testing"], ["created_at", 2016-07-13 21:30:45 UTC], ["updated_at", 2016-07-13 21:30:45 UTC]]
    (2.9ms) COMMIT
    #<Person id: 37, name: "Testing", age: nil, adult: nil, created_at: "2016-07-13 21:30:45", updated_at: "2016-07-13 21:30:45">

    此页面显示了在 Rails 5 中进行了哪些更改以具有此行为。

    http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html

    关于ruby-on-rails-5 - rails 5 accepts_nested_attributes 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38360271/

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