Ruby 无法使用用户创建配置文件
用户型号:Ruby 无法使用用户创建配置文件,ruby,ruby-on-rails-5,Ruby,Ruby On Rails 5,用户型号: has_one :profile before_create :build_profile # Works when using form views 配置文件模型 belongs_to :user 我在profiles表上有user\u id,因此所有设置都在那里 我想从控制台创建一个配置文件为的用户: User.create!(....).build_profile(...) 我只获得用户,但没有配置文件 User.new(...).build_profile(...).s
has_one :profile
before_create :build_profile # Works when using form views
配置文件模型
belongs_to :user
我在profiles表上有user\u id
,因此所有设置都在那里
我想从控制台创建一个配置文件为的用户:
User.create!(....).build_profile(...)
我只获得用户,但没有配置文件
User.new(...).build_profile(...).save
#=> can't modify frozen Hash
怎么做呢?我确信我所做的是正确的。是设备导致问题吗?我执行了相同的操作,只是做了一些小的更改,并且工作正常
User.create!(name: 'Test user', email: "testuser@gmail.com", password: "password").build_profile.save
执行了以下查询
BEGIN
User Exists (35.8ms) SELECT 1 AS one FROM `users` WHERE (`users`.`email` = BINARY 'testuser@gmail.com' AND `users`.`deleted_at` IS NULL) LIMIT 1
SQL (7.0ms) INSERT INTO `users` (`name`, `email`, `encrypted_password`, `created_at`, `updated_at`) VALUES ('Test user', 'testuser@gmail.com', '$2a$10$adX3zznn6R5G94WY5CtCZOHAVYZCXBD/ASDdyoDU7jl8P9RmRJUmC', '2016-12-01 13:41:07', '2016-12-01 13:41:07')
(2.6ms) COMMIT
Profile Load (0.4ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = '43' LIMIT 1
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO `profiles` (`user_id`, `created_at`, `updated_at`) VALUES ('43', '2016-12-01 13:41:07', '2016-12-01 13:41:07')
(1.7ms) COMMIT => true
下面的模型代码包含最少的数据
class User < ActiveRecord::Base
has_one :profile
end
class用户
剖面模型
class Profile < ActiveRecord::Base
belongs_to :user
end
类配置文件
如果Desive将为您创建问题,则在保存用户对象时,它必须在控制台中抛出验证错误
编辑:配置文件迁移文件
class CreateProfiles < ActiveRecord::Migration
def change
create_table :profiles do |t|
t.string :user_id
t.string :Description
t.timestamps null: false
end
end
end
class CreateProfiles
我执行了相同的操作,只是做了一些小的改动,效果与预期一致
User.create!(name: 'Test user', email: "testuser@gmail.com", password: "password").build_profile.save
执行了以下查询
BEGIN
User Exists (35.8ms) SELECT 1 AS one FROM `users` WHERE (`users`.`email` = BINARY 'testuser@gmail.com' AND `users`.`deleted_at` IS NULL) LIMIT 1
SQL (7.0ms) INSERT INTO `users` (`name`, `email`, `encrypted_password`, `created_at`, `updated_at`) VALUES ('Test user', 'testuser@gmail.com', '$2a$10$adX3zznn6R5G94WY5CtCZOHAVYZCXBD/ASDdyoDU7jl8P9RmRJUmC', '2016-12-01 13:41:07', '2016-12-01 13:41:07')
(2.6ms) COMMIT
Profile Load (0.4ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = '43' LIMIT 1
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO `profiles` (`user_id`, `created_at`, `updated_at`) VALUES ('43', '2016-12-01 13:41:07', '2016-12-01 13:41:07')
(1.7ms) COMMIT => true
下面的模型代码包含最少的数据
class User < ActiveRecord::Base
has_one :profile
end
class用户
剖面模型
class Profile < ActiveRecord::Base
belongs_to :user
end
类配置文件
如果Desive将为您创建问题,则在保存用户对象时,它必须在控制台中抛出验证错误
编辑:配置文件迁移文件
class CreateProfiles < ActiveRecord::Migration
def change
create_table :profiles do |t|
t.string :user_id
t.string :Description
t.timestamps null: false
end
end
end
class CreateProfiles
是但否个人资料上的用户id
。这是我现在的问题。您必须使用调用。保存。创建代码>?是的,使用这个,它会工作。我将使用配置文件迁移文件更新我的ans是,但不更新配置文件上的user\u id
。这是我现在的问题。您必须使用调用。保存。创建代码>?是的,使用这个,它会工作。我将使用配置文件迁移文件更新我的ans