仅在rails 5 api应用程序中处理嵌套属性

时间:2018-05-30 10:13:57

标签: rails-api ruby-on-rails-5.2

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

class Client < ApplicationRecord
    has_many :orders
end

我的订单.rb是

class Order < ApplicationRecord
  belongs_to :client, dependent: :destroy
  accepts_nested_attributes_for :client
  validates_presence_of :order_amount, :service_amount, :miners_amount
end

我有一条路线暴露 / place_order ,并创建了客户和订单。

class OrderProcessingController < ApplicationController
    def place_order
        @order = Order.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
end

到目前为止一切正常。现在我的要求是,我必须检查客户端表中是否已存在客户端。如果是,则为订单添加client_id并创建新订单。我不想每次都创建新的客户和订单。

我怎样才能在before_filter或类似的东西中实现相同的目标。从客户端参数获取客户端,如果客户端存在从传入的参数中删除params键???

place_order的发布数据如下

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

提前致谢, 阿吉特

2 个答案:

答案 0 :(得分:1)

以下代码未经过测试,主要是您的方法应该围绕此

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

答案 1 :(得分:0)

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

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

谢谢, 阿吉特