Ruby on rails 用它的关系创建模型

Ruby on rails 用它的关系创建模型,ruby-on-rails,ruby-on-rails-5,rails-activerecord,Ruby On Rails,Ruby On Rails 5,Rails Activerecord,我正在尝试将我的项目从Rails 5.0迁移到5.2 该项目广泛使用通过.build_u%related_umodel%创建相关模型,它在rails 5.0上工作,现在已经破产了。 是否删除了此功能,还是应该使用其他语法 class User < ActiveRecord::Base belongs_to :profile, inverse_of: :user end class Profile < ActiveRecord::Base has_one :user, inv

我正在尝试将我的项目从Rails 5.0迁移到5.2

该项目广泛使用通过.build_u%related_umodel%创建相关模型,它在rails 5.0上工作,现在已经破产了。 是否删除了此功能,还是应该使用其他语法

class User < ActiveRecord::Base
  belongs_to :profile, inverse_of: :user
end

class Profile < ActiveRecord::Base
  has_one :user, inverse_of: :profile
end

new_user = User.new
new_user.build_profile
new_user.save
在此之前,此代码创建了用户及其配置文件。现在,这将只创建用户,而不创建配置文件

有没有办法解决这个问题

irb(main):001:0> new_user = User.new
=> #<User id: nil, profile_id: nil, created_at: nil, updated_at: nil>
irb(main):002:0> new_user.build_profile
=> #<Profile id: nil, created_at: nil, updated_at: nil>
irb(main):003:0> new_user.save
   (0.1ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "profiles" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2018-04-21 13:16:23.669286"], ["updated_at", "2018-04-21 13:16:23.669286"]]
  Profile Load (0.2ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  SQL (0.2ms)  INSERT INTO "users" ("profile_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["profile_id", 1], ["created_at", "2018-04-21 13:16:23.691292"], ["updated_at", "2018-04-21 13:16:23.691292"]]
   (181.1ms)  commit transaction
=> true
class CreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.integer :profile_id
      t.timestamps
    end
  end
end
class CreateProfiles < ActiveRecord::Migration[5.1]
  def change
    create_table :profiles do |t|

      t.timestamps
    end
  end
end
class User < ApplicationRecord
  belongs_to :profile, inverse_of: :user
end
class Profile < ApplicationRecord
  has_one :user, inverse_of: :profile
end
测试结果表明一切正常。复制了要应答的代码,因为它在注释中不可读。您的问题一定在其他地方您是否收到任何错误或将迁移文件粘贴到问题中

接受\u嵌套的\u属性\u for:profile修复了这个问题,但出现了许多其他问题


我必须停止此更新并回滚所有内容。

也许您的配置文件模型已更改了新属性,添加了构建它所需的内容?。您的配置文件迁移文件是什么?@ImreRaudsepp model保存时没有任何错误。它只是没有保存相关的模型。同样,这段代码以前工作得很好。你只更新了rails版本,没有更改任何其他内容,而且它不再工作了。Gems和Ruby版本也一样。尝试检查构建的配置文件的有效性:profile=new_user.build_profile;个人资料。有效吗?和profile.errors(如果它不有效)使用Rails 5.1.6