Rails api 处理Rails5API专用应用程序中的嵌套属性

Rails api 处理Rails5API专用应用程序中的嵌套属性,rails-api,ruby-on-rails-5.2,Rails Api,Ruby On Rails 5.2,我对rails api应用程序有一个要求。客户可以有多个订单,每个订单属于一个客户 class Client < ApplicationRecord has_many :orders end 提前感谢,, Ajith下面的代码没有经过测试,大部分情况下您的方法都应该围绕这一点 class OrderProcessingController < ApplicationController before_action :find_client, only: [:place_

我对rails api应用程序有一个要求。客户可以有多个订单,每个订单属于一个客户

class Client < ApplicationRecord
    has_many :orders
end
提前感谢,,
Ajith

下面的代码没有经过测试,大部分情况下您的方法都应该围绕这一点

class OrderProcessingController < ApplicationController
  before_action :find_client, only: [:place_order]

  def place_order
    @order = @client.orders.new(order_processing_params)

    if @order.save
        render json: @order
    else
        render json: @order.errors.full_messages
    end
  end

private
  def order_processing_params
    params.require(:order).permit(:order_amount, :service_amount, :miners_amount, client_attributes: [:name, :email, :phone_number, :date])
  end

  def find_client
    @client = Client.find_or_create_by(email: params[:order][:client_attributes][:email])
    #below line can be improved using a method, see the last line if later you want, never update a primary key which is email in bulk params
    @client.update_attributes(name: params[:order][:client_attributes][:name], phone_number: params[:order][:client_attributes][:phone_number], date: params[:order][:client_attributes][:date])
  end

  #def get_client_params
   # params.require(:order)
  #end
end

我尝试了下面的方法来获得解决方案。我不太确定这是解决问题的正确方法

class OrderProcessingController < ApplicationController
    before_action :find_client, only: :place_order
    def place_order
        if @client.present?
            @order = @client.orders.build(order_processing_params) 
        else
            @order = Order.new(order_processing_params)

        end
        if @order.save
            render json: @order
        else
            render json: @order.errors.full_messages
        end
    end

    private
    def order_processing_params
        params.require(:order).permit(:order_amount, :service_amount, :miners_amount, client_attributes: [:name, :email, :phone_number, :date])
    end

    def find_client
        begin
            @client =  Client.find_by_email(params[:order][:client_attributes][:email])
        rescue
            nil
        end
    end
end
谢谢,
Ajith

是否必须每次传递客户端属性而不是客户端id?客户端的主键是什么。电子邮件?参数将始终具有相同的结构。每次我下订单时,都必须检查客户记录。如果未找到客户机,则向客户机表和订单表添加条目。假设客户已经存在并下订单。为订单设置客户端id。无需每次都创建新条目。关系:客户有很多订单。客户端表主键是电子邮件。感谢bijendra。我考虑了你的建议。
{
     "order" : {
       "order_amount" : "10000",
        "service_amount" : "1000",
        "miners_amount" : "10000",
         "client_attributes": {
                "name": "Ajith",
                "email": "ajith@gmail.com",
                "phone_number": "12231321312",
                "date": "12/12/12"
            }
         }
  }
class OrderProcessingController < ApplicationController
  before_action :find_client, only: [:place_order]

  def place_order
    @order = @client.orders.new(order_processing_params)

    if @order.save
        render json: @order
    else
        render json: @order.errors.full_messages
    end
  end

private
  def order_processing_params
    params.require(:order).permit(:order_amount, :service_amount, :miners_amount, client_attributes: [:name, :email, :phone_number, :date])
  end

  def find_client
    @client = Client.find_or_create_by(email: params[:order][:client_attributes][:email])
    #below line can be improved using a method, see the last line if later you want, never update a primary key which is email in bulk params
    @client.update_attributes(name: params[:order][:client_attributes][:name], phone_number: params[:order][:client_attributes][:phone_number], date: params[:order][:client_attributes][:date])
  end

  #def get_client_params
   # params.require(:order)
  #end
end
class OrderProcessingController < ApplicationController
    before_action :find_client, only: :place_order
    def place_order
        if @client.present?
            @order = @client.orders.build(order_processing_params) 
        else
            @order = Order.new(order_processing_params)

        end
        if @order.save
            render json: @order
        else
            render json: @order.errors.full_messages
        end
    end

    private
    def order_processing_params
        params.require(:order).permit(:order_amount, :service_amount, :miners_amount, client_attributes: [:name, :email, :phone_number, :date])
    end

    def find_client
        begin
            @client =  Client.find_by_email(params[:order][:client_attributes][:email])
        rescue
            nil
        end
    end
end