Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql rails 4使用一些散列修改查询输出_Sql_Database_Postgresql_Ruby On Rails 4_Params - Fatal编程技术网

Sql rails 4使用一些散列修改查询输出

Sql rails 4使用一些散列修改查询输出,sql,database,postgresql,ruby-on-rails-4,params,Sql,Database,Postgresql,Ruby On Rails 4,Params,这是我从查询数据库输出的示例数据 {name: "abc", product_id: 1, supplier_id: 1, supplier_name: "zxc", quantity:"40", price:"100"} 现在在rails中,可以这样修改输出 {name: "abc", product_id: 1, supplier_id: 1, details: {supplier_name: "zxc", quantity:"40", price:"100"} } 我如何做到这一点,更

这是我从查询数据库输出的示例数据

{name: "abc", product_id: 1, supplier_id: 1, supplier_name: "zxc", quantity:"40", price:"100"}
现在在rails中,可以这样修改输出

{name: "abc", product_id: 1, supplier_id: 1, details: {supplier_name: "zxc", quantity:"40", price:"100"} }
我如何做到这一点,更改一些字段并将其插入哈希,我使用它是因为它用于显示详细信息页面

非常感谢您的简单评论和回答,, 多谢各位

编辑1

这是我的控制器

  def crud_show(model)
if model.methods.include?(:show)
  data = model.show(params[:id], current_holding_company.id)
else
  data = model.find_by(id: params[:id], holding_company_id)
end

if data.blank?
  render nothing: true, status: 404
else
  respond_to do |format|
    format.json { render json: data.to_json, status: 200 }
  end
end
end
这是我的模型

    validates :holding_company_id, presence: true
validates :currency_id, presence: true
validates :supplier_id, presence: true
validates :number, presence: true
validates :shipment_date, presence: true
validates :type_of_payment, presence: true
validates :rate, presence: true
validates :subtotal, presence: true
validates :subtotal_idr, presence: true
validates :discount, presence: true
validates :discount_idr, presence: true
validates :down_payment, presence: true
validates :down_payment_idr, presence: true
validates :total, presence: true
validates :total_idr, presence: true
validates :transaction_date, presence: true

belongs_to :holding_company
belongs_to :supplier
belongs_to :currency
has_many :receiving_from_supplier_details
has_many :receiving_from_supplier_return_to_suppliers

accepts_nested_attributes_for :receiving_from_supplier_details

# CALLBACKS
after_initialize :set_default_value
before_validation :set_detail

# VIRTUAL ATTRIBUTES
attr_accessor :details, :warehouse_id
attr_reader :receiving_from_supplier_details_attributes

def self.virtual_attributes
[:receiving_from_supplier_details_attributes, :details, :warehouse_id]
end

def self.show(id, holding_company_id)

details = joins('LEFT JOIN receiving_from_supplier_details ON receiving_from_supplier_details.receiving_from_supplier_id = receiving_from_suppliers.id LEFT JOIN product_barcodes ON product_barcodes.id = receiving_from_supplier_details.product_barcode_id LEFT JOIN products ON products.id = product_barcodes.product_id').select('receiving_from_suppliers.*, receiving_from_supplier_details.quantity as receiving_from_supplier_details_quantity, product_barcodes.product_id as product_barcodes_product_id, products.name as products_name, products.code as products_code, receiving_from_supplier_details.price as receiving_from_supplier_details_price, receiving_from_supplier_details.expire_date as receiving_from_supplier_details_expire_date').group('receiving_from_suppliers.id, receiving_from_supplier_details.quantity, product_barcodes.product_id, products.code, products.name, receiving_from_supplier_details.price, receiving_from_supplier_details.expire_date')

if self.column_names.include? "holding_company_id"
  result = details.find_by(id: id, holding_company_id: holding_company_id)
else
  result = details.find_by(id: id)
end

result = result.attributes if result.present?
end

protected

def set_detail
  self.shipment_date = self.transaction_date if self.shipment_date.blank?
  if self.details.present?
    temp_detail = []
    self.details.each do |unused_index, details|
      details["warehouse_id"] = self.warehouse_id unless details.keys.include? 'warehouse_id'
      details["holding_company_id"] = self.holding_company_id unless details.keys.include? 'holding_company_id'
      temp_detail << details
    end
    self.receiving_from_supplier_details_attributes = temp_detail
  end
end

def set_default_value
  self.currency_id = Currency.first.id
  self.type_of_payment = CASH_PAYMENT
end
验证:持有公司id,状态:true
验证:货币id,状态:true
验证:供应商id,存在:true
验证:编号,状态:真
验证:装运日期,存在:真
验证:付款类型,状态:true
验证:速率,状态:真
验证:小计,存在:真
验证:小计\u idr,存在:真
验证:折扣,状态:真
验证:折扣\u idr,状态:true
验证:首付,状态:真
验证:首付款\u idr,状态:true
验证:总计,存在:真
验证:总idr,状态:true
验证:事务处理日期,状态:true
属于:控股公司
属于:供应商
属于:货币
有很多:从供应商处收到详细信息
是否有多个:从供应商处接收\u返回\u给供应商
接受\u嵌套的\u属性\u用于:从\u供应商\u详细信息接收\u
#回调
初始化后:设置默认值
验证前:设置详细信息
#虚拟属性
属性存取器:详细信息,:仓库id
属性读取器:从供应商处接收属性详细信息
def self.virtual_属性
[:从供应商处接收\详细信息\属性,:详细信息,:仓库\ id]
结束
定义自显示(id、控股公司id)
详细信息=加入(“左加入接收来自供应商的详细信息关于接收来自供应商的详细信息。接收来自供应商的详细信息。id=接收来自供应商的详细信息。id左加入产品产品产品条形码产品条形码。id=接收来自供应商的详细信息。产品条形码\id左加入产品。id=产品条形码。产品id')。选择('receiving_from_suppliers.*,receiving_from_supplier_details.quantity as receiving_from_supplier_details_数量,product_条形码,product_id as product_条形码,products.name as products_name,products.code as products_Codes,receiving_from_supplier_详情,price as_价格,receiving_from supplier_详情ls.expire\u date as receiving\u from\u supplier\u details\u expire\u date')。组('receiving\u from\u suppliers.id,receiving\u from\u supplier\u details.quantity,product\u barcode.product\u id,product.code,product.name,receiving\u from\u supplier\u details.price,receiving\u from\u supplier\u details.expire
如果self.column\u names.include?“控股公司\u id”
结果=详细信息。查找人(id:id,控股公司\U id:控股公司\U id)
其他的
结果=详细信息。查找依据(id:id)
结束
如果result.present存在,则result=result.attributes?
结束
受保护的
def set_详细信息
self.shipping\u date=self.transaction\u date如果self.shipping\u date.blank?
如果self.details.present?
临时详细信息=[]
self.details.each do |未使用的|索引,详细信息|
details[“warehouse\u id”]=self.warehouse\u id,除非details.key.include?'warehouse\u id'
详细信息[“控股公司id”]=self.holding\u company\u id,除非是details.key.include?“控股公司id”

如果没有更多的细节和了解您的模型以及您当前使用的查询,temp_detail将无法回答。如果您想要一个好的答案,请进行腿部工作并发布更多细节。这将帮助您获得答案。我已经添加了您所需的所有内容,我希望它能帮助您找到解决方案??