Merge different Methods Querys into a single JSON

Asked

Viewed 32 times

0

I’m trying to create a select where returns data in json and then created a method that would be a "database" that is another select bringing a result, as I would to juxtapose all this and bring a json only?

    class ConsolidatedOrdersController < ApplicationController
  def index
    json_date = []

    dt_inicio = fc_inicio(931101)

    json_date = Order.left_outer_joins(:load, :user, :pallet_kind, :order_priority, step: [:status, :role])
                     .select('orders.*','loads.*', 'statuses.name as Status', 'pallet_kinds.name as Tipo_de_Pallet', 'order_priorities.name as Prioridade')
                     .where('orders.step' => 500)
                     .as_json

    render json: json_date
  end

  def fc_inicio(order_id, status_id = 3, role_id = 1)
    OrderHistory.joins(:step)
                .select('order_histories.start')
                .where('steps.role_id' => role_id, 'steps.status_id' => status_id, 'order_histories.order_id' => order_id)
                .where.not(end: nil)
                .limit(1)
                .to_json
  end
  • 1

    Mano, use the merge() method according to: https://apidock.com/ruby/Hash/merge If the two methods return a json,at the end of one of the methods just put . merge (other-way-name). Remembering that if the two methods have keys of equal names, it will replace the old one with the new one being merged.

1 answer

0

json_unido = fc_inicio.merge(json_date)

Just be careful because repeated keys can be overwritten

Browser other questions tagged

You are not signed in. Login or sign up in order to post.