rails 4用一些哈希修改查询输出

时间:2014-10-08 10:19:25

标签: 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"} }

我怎么能做到这一点,更改一些字段并将其插入哈希,我使用它,因为它用于显示详细信息页面,

一个简单的评论和答案将非常感激, 谢谢

编辑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

并且没有任何视图,因为我正在使用json并使用rspec

进行测试

0 个答案:

没有答案