如何将参数传递到Rails中的两个不同表

 收藏

作为一个新手,我开始进行API POC。我有一种情况,如下所述:

我有一个具有create方法的seekerController。我希望当一个Post请求发出请求时,很少有参数需要进入seeker表,而很少有需要进入profile表(该表也有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

让我在这里提出我的问题: 我有如下的正文请求参数:

{
      "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

我已经尝试过在开始代码中发布的内容,但是由于seekerprofile_params为空,因此出现了错误。所以我确信我的方法是错误的。

谁能提供示例代码,该怎么做?我是java的家伙,所以对ruby比较新鲜。

回复
  • qq情侣网名 回复

    With the limited information that is given, it seems as though the problem could be related to the seeker_id field being blank in the result of seekerprofile_params. Basically, we're setting params[:seeker_id] as params[:seeker_id] = seeker[:id] after saving Seeker. But while creating params for creating SeekerProfile, we use seekerprofile_params which looks for seeker_id in params[:seeker_profile][:seeker_id] since we use params.require(:seeker_profile) before permitting seeker_id. Since SeekerProfile does not get a seeker_id, it may not get saved depending on how the model is setup.
    However, in case you're trying to create both, a Seeker as well as a SeekerProfile, you may want to check out nested attributes in Rails.

    Edit after receiving more inputs:

    Considering that the API contract cannot change and needs to be maintained, the following approach could be used to create a seeker and a seeker_profile:
    1) We could change the model Seeker to accept nested attributes for SeekerProfile as follows:

    # 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
    

    What happens here is basically we allow the seeker model to accept attributes for seeker_profiles during creation. These attributes are accepted by the model using the seeker_profiles_attributes attribute writer. Since the relationship is a has_many relationship, seeker_profiles_attributes accepts an array of objects, where each hash object represents one seeker_profile child to be created.
    In the code mentioned above, I've assumed that only one seeker_profile is to be created. In case your API changes and wants to accept multiple profiles during creation, I would leave that upto you to figure out, with the assurance that you could get back in the comments in case you're stuck.
    Another thing to note is that the ActiveRecord::Base.transaction block is not required since failure in any of the objects being created would rollback the entire transaction anyway.