如何将参数传递给 rails 中的两个不同表
How to pass the params to two different tables in rails
作为新手,我开始做 API POC。我的情况如下所述:
我有创建 method.I 的 seekerController 希望当一个 Post 请求发出时,很少有参数需要去 seeker table 并且很少需要去 profile table (这个 table 也有 seekerID 列)。我想在事务提交中执行此操作。所以看完之后我开始做下面的事情:-
ActiveRecord::Base.transaction do
seeker = Seeker.new(seeker_params)
seeker.save!
params[:seeker_id] = seeker[:id]
seekerprofile = SeekerProfile.new(seekerprofile_params)
seekerprofile.save!
end
render json: {status: 'success', message: 'Request is processed successully', data:seeker},status: :created;
我有以下定义:(我怀疑下面的方法是否正确)
def seeker_params
params.require(:seeker).permit(:username, :alias, :mobile_number, :country_code, :email_address, :description, :status)
end
def seekerprofile_params
params.require(:seeker_profile).permit(:seeker_id, :first_name, :middle_name, :last_name, :date_of_birth, :pincode, :building_name, :address, :email_address, :description, :status)
end
让我在这里直接提出我的问题:-
我有 post 正文请求参数,如下所示:
{
"username" : "TestName12",
"alias" : "TestAlia12",
#above should go to seeker table
"first_name":"xyz",
"Last_Name":"abc"
#above should go above Seekerprofile table. seekerprofile has seekerid also.
}
我的模型如下:-
> class SeekerProfile < ApplicationRecord
>
> belongs_to :seeker end
我已尝试 post 开始代码中的内容,但我收到错误消息,因为 seekerprofile_params 为空。所以我确定我的方法是错误的。
谁能提供示例代码,怎么做?我是 java 人,ruby.
更新鲜
根据所提供的有限信息,问题似乎与 seekerprofile_params
结果中的 seeker_id
字段为空白有关。基本上,我们在保存 Seeker
后将 params[:seeker_id]
设置为 params[:seeker_id] = seeker[:id]
。但是在创建用于创建 SeekerProfile
的参数时,我们使用 seekerprofile_params
在 params[:seeker_profile][:seeker_id]
中查找 seeker_id
因为我们在允许 seeker_id
之前使用 params.require(:seeker_profile)
。由于 SeekerProfile
没有得到 seeker_id
,它可能无法保存,具体取决于模型的设置方式。
但是,如果您尝试同时创建 Seeker
和 SeekerProfile
,您可能需要查看 nested attributes in Rails.
收到更多意见后编辑:
考虑到 API 合约无法更改且需要维护,可以使用以下方法创建 seeker
和 seeker_profile
:
1) 我们可以更改模型 Seeker
以接受 SeekerProfile
的嵌套属性,如下所示:
# app/models/seeker.rb
has_many :seeker_profiles # As mentioned in the question comments
accepts_nested_attributes_for :seeker_profiles
2) 然后可以如下更改控制器代码:
# app/controllers/seeker_controller.rb
def create
seeker = Seeker.new(creation_params)
seeker.save!
render json: {status: 'success', message: 'Request is processed successully', data:seeker},status: :created
end
private
def creation_params
params.permit(:username, :alias).merge(seeker_profiles_attributes: [seeker_profile_creation_params])
end
def seeker_profile_creation_params
params.permit(:first_name, :last_name)
end
这里发生的基本上是我们允许 seeker
模型在创建期间接受 seeker_profiles
的属性。模型使用 seeker_profiles_attributes
属性编写器接受这些属性。由于关系是 has_many
关系,seeker_profiles_attributes
接受一个对象数组,其中每个哈希对象代表一个要创建的 seeker_profile
个子对象。
在上面提到的代码中,我假设只创建一个 seeker_profile
。如果您的 API 发生变化并希望在创建过程中接受多个配置文件,我会让您自己解决这个问题,并保证您可以在遇到困难时返回评论。
另一件需要注意的事情是 ActiveRecord::Base.transaction
块不是必需的,因为正在创建的任何对象的失败都会回滚整个事务。
作为新手,我开始做 API POC。我的情况如下所述:
我有创建 method.I 的 seekerController 希望当一个 Post 请求发出时,很少有参数需要去 seeker table 并且很少需要去 profile table (这个 table 也有 seekerID 列)。我想在事务提交中执行此操作。所以看完之后我开始做下面的事情:-
ActiveRecord::Base.transaction do
seeker = Seeker.new(seeker_params)
seeker.save!
params[:seeker_id] = seeker[:id]
seekerprofile = SeekerProfile.new(seekerprofile_params)
seekerprofile.save!
end
render json: {status: 'success', message: 'Request is processed successully', data:seeker},status: :created;
我有以下定义:(我怀疑下面的方法是否正确)
def seeker_params
params.require(:seeker).permit(:username, :alias, :mobile_number, :country_code, :email_address, :description, :status)
end
def seekerprofile_params
params.require(:seeker_profile).permit(:seeker_id, :first_name, :middle_name, :last_name, :date_of_birth, :pincode, :building_name, :address, :email_address, :description, :status)
end
让我在这里直接提出我的问题:- 我有 post 正文请求参数,如下所示:
{
"username" : "TestName12",
"alias" : "TestAlia12",
#above should go to seeker table
"first_name":"xyz",
"Last_Name":"abc"
#above should go above Seekerprofile table. seekerprofile has seekerid also.
}
我的模型如下:-
> class SeekerProfile < ApplicationRecord
>
> belongs_to :seeker end
我已尝试 post 开始代码中的内容,但我收到错误消息,因为 seekerprofile_params 为空。所以我确定我的方法是错误的。
谁能提供示例代码,怎么做?我是 java 人,ruby.
更新鲜根据所提供的有限信息,问题似乎与 seekerprofile_params
结果中的 seeker_id
字段为空白有关。基本上,我们在保存 Seeker
后将 params[:seeker_id]
设置为 params[:seeker_id] = seeker[:id]
。但是在创建用于创建 SeekerProfile
的参数时,我们使用 seekerprofile_params
在 params[:seeker_profile][:seeker_id]
中查找 seeker_id
因为我们在允许 seeker_id
之前使用 params.require(:seeker_profile)
。由于 SeekerProfile
没有得到 seeker_id
,它可能无法保存,具体取决于模型的设置方式。
但是,如果您尝试同时创建 Seeker
和 SeekerProfile
,您可能需要查看 nested attributes in Rails.
收到更多意见后编辑:
考虑到 API 合约无法更改且需要维护,可以使用以下方法创建 seeker
和 seeker_profile
:
1) 我们可以更改模型 Seeker
以接受 SeekerProfile
的嵌套属性,如下所示:
# app/models/seeker.rb
has_many :seeker_profiles # As mentioned in the question comments
accepts_nested_attributes_for :seeker_profiles
2) 然后可以如下更改控制器代码:
# app/controllers/seeker_controller.rb
def create
seeker = Seeker.new(creation_params)
seeker.save!
render json: {status: 'success', message: 'Request is processed successully', data:seeker},status: :created
end
private
def creation_params
params.permit(:username, :alias).merge(seeker_profiles_attributes: [seeker_profile_creation_params])
end
def seeker_profile_creation_params
params.permit(:first_name, :last_name)
end
这里发生的基本上是我们允许 seeker
模型在创建期间接受 seeker_profiles
的属性。模型使用 seeker_profiles_attributes
属性编写器接受这些属性。由于关系是 has_many
关系,seeker_profiles_attributes
接受一个对象数组,其中每个哈希对象代表一个要创建的 seeker_profile
个子对象。
在上面提到的代码中,我假设只创建一个 seeker_profile
。如果您的 API 发生变化并希望在创建过程中接受多个配置文件,我会让您自己解决这个问题,并保证您可以在遇到困难时返回评论。
另一件需要注意的事情是 ActiveRecord::Base.transaction
块不是必需的,因为正在创建的任何对象的失败都会回滚整个事务。