根据最接近的特定价格编号的订单记录?

时间:2018-07-26 17:12:57

标签: ruby-on-rails

我需要在产品展示页面上提供类似产品价格的建议。

所以考虑:

#iterative function     
for row in ids:
 cursor.execute('SELECT PRIMARY_EQUIP_FED_FROM, ISPANEL FROM PANELBOARDS 
 WHERE PANE_ID = :panel', {'panel': row[0]})
 prime = cursor.fetchone()
 panelRecursion(prime, prime[1], row[0])
 columnTracker = 1
 rowTracker += 1
 wb.save('demo.xlsx')

#Recursive Function
def panelRecursion(primary, isPanelFlag, initial):
 global columnTracker
 if primary is None:
    return
 if isPanelFlag == 0 and initial == 0:
    cursor.execute('SELECT NAME FROM ELECTRICAL_EQUIPMENTS WHERE ELEQ_ID = :panel', {'panel': primary[0]})
    eName = cursor.fetchone()
    if eName is None:
        return
    if "LCR" in eName[0]:
        sheet.cell(row=rowTracker, column=columnTracker).value = eName[0]
        columnTracker += 1
        return


 if isPanelFlag or initial > 0:
    panel = {'panel': primary[0]}
    p2 = primary[0]
    if initial > 0:
        panel = {'panel': initial}
        p2 = initial
        initial = 0
    cursor.execute('SELECT PRIMARY_EQUIP_FED_FROM, ISPANEL FROM PANELBOARDS WHERE PANE_ID = :panel', panel)
    primary = cursor.fetchone()
    panelRecursion(primary, primary[1], 0)
    columnTracker += 1
    cursor.execute('SELECT NAME FROM PANELBOARDS WHERE PANE_ID = :panel', {'panel': p2})
    eName = cursor.fetchone()
    if eName is None:
        return
    sheet.cell(row=rowTracker, column=columnTracker).value = eName[0]
    return False

 else:
    panel = {'panel': primary[0]}
    p2 = primary[0]
    cursor.execute(
        'SELECT PRIMARY_EQUIP_FED_FROM, PRIMARY_ISPANEL FROM ELECTRICAL_EQUIPMENTS WHERE ELEQ_ID = :panel', panel)
    primary = cursor.fetchone()
    panelRecursion(primary, primary[1], 0)
    columnTracker += 1
    cursor.execute('SELECT NAME FROM ELECTRICAL_EQUIPMENTS WHERE ELEQ_ID = :panel', {'panel': p2})
    eName = cursor.fetchone()
    if eName is None:
        return
    sheet.cell(row=rowTracker, column=columnTracker).value = eName[0]
    return

如何获得与该价格最接近的第十个产品记录?

类似的东西:

@products = Product.all
@specific_product = Product.all.find(20)
@price = @specific_product.price

1 个答案:

答案 0 :(得分:0)

this post回答了问题。

所以我必须做:

@products = Products.all.order("abs(price - #{@price})")