سؤال

لدي هذا الاستعلام القبيح الضخم أدناه، وأود أن أقوم بالفرز حسبه في عرض الكتالوج.التفكير بشيء مثل http://wow.dev:3000/catalog_items?&order=deals.مليون شكر مقدما على أي تعليقات أو إجابات.

select 100 - round((current_price / item.estimated_price)*100) as percent, item.cached_thumbnail_url, item.item_id, it.name,
          ci.current_price, ci.close_date
           from item
           join catalog_item ci on ci.item_id = item.item_id
           join item_translations as it on (it.item_id = item.item_id)
           where  (100 - round((current_price / item.estimated_price)*100)) > 49 and 
           item.estimated_price > 0 and ci.current_price > 0 and ci.close_date > now() and item.active = 1 and ci.active = 1 and 
           (current_price / estimated_price) < 1
           order by (ci.close_date < DATE_ADD(now(), INTERVAL 17 hour))  and (item.estimated_price - current_price) desc
           limit 12
هل كانت مفيدة؟

المحلول

لست متأكدًا من كيفية ارتباط ذلك بـ ROR، ولكن على أي حال:

  1. لديك 100 - round((current_price / item.estimated_price)*100) as percent في جملة SELECT الخاصة بك، ولكن على أي حال باستخدام نفس التعبير في شروط WHERE.

  2. يستطيع item.estimated_price يكون أقل من الصفر؟إذا لم يكن item.estimated_price > 0 الشرط مفرط، إذا كان صفر (100 - round((current_price / item.estimated_price)*100)) > 49 الشرط سيكون كاذبا

  3. ال (current_price / estimated_price) < 1 مبالغ فيه لنفس الأسباب

لذلك يمكن إعادة كتابة استعلامك بشكل أكثر وضوحًا مثل هذا:

select (100 - round((current_price / item.estimated_price)*100)) as percent,
   item.cached_thumbnail_url, item.item_id, it.name,
   ci.current_price, ci.close_date
from item
   join catalog_item ci on ci.item_id = item.item_id
   join item_translations as it on (it.item_id = item.item_id)
where
   percent > 49
   and ci.current_price > 0
   and ci.close_date > now()
   and item.active = 1
   and ci.active = 1
order by
   (ci.close_date < DATE_ADD(now(), INTERVAL 17 hour))
   and (item.estimated_price - current_price) desc
limit 12

هذا لا يحسن الوضع كثيرًا، لكن لا يمكنني قول المزيد دون معرفة المزيد من الأسباب حول بنية قاعدة البيانات الخاصة بك.

راجع للشغل الرابط في سؤالك لا يعمل (من الواضح أنه الرابط المحلي الخاص بك)

نصائح أخرى

بناءً على إجابة الكود الثنائي، يمكنك محاولة تغليف استعلامك في ARel، ثم في إجراء وحدة التحكم الخاصة بك، يمكنك طلب الحقل الذي تم تمريره في تجزئة المعلمات، شيء من هذا القبيل:

class ItemsController < ApplicationController
  ...
  def index
    @items = Item.select(%{(100 - round((current_price / item.estimated_price)*100)) as percent,
               item.cached_thumbnail_url, item.item_id, it.name,
               ci.current_price, ci.close_date}).
             joins('join catalog_item ci on ci.item_id = item.item_id').
             joins('join item_translations as it on (it.item_id = item.item_id)').
             where('percent > 49 and ci.current_price > 0 and ci.close_date > now() and item.active = 1 and ci.active = 1').
             order(%{(ci.close_date < DATE_ADD(now(), INTERVAL 17 hour))
               and (item.estimated_price - current_price) desc)}).limit(12)
    @items = @items.order(params[:order]) if params[:order]
    ...
end

يحرر

كما يشير الرمز الثنائي أدناه، قد ترغب في جعل كود وحدة التحكم أكثر نظافة عن طريق نقل الاستعلام الرئيسي خارج الإجراء، ربما إلى طريقة في Item النموذج، والتأكد من أنك لا تزال تعود أ Relation كائن منه (كما يفعل البيان الحالي)، حتى تتمكن من ربط الإضافات order لاحقاً:

def index
  @items = Item.by_price_ratio
  @items = @items.order(params[:order]) if params[:order]
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top