比较两个查询中的值并在Ruby中返回匹配的值

时间:2019-05-31 05:09:31

标签: ruby-on-rails ruby

我需要从两个单独的查询中获取模型的ID,并检查两个查询中是否存在相同的ID。如果不是,那么我必须传递字符串Not present,无论它不在哪里,否则我都必须传递值。

class Purchase
    belongs_to :customer
    belongs_to :selected_items

class SelectedItem
    has_many :purchases
    has_many :sub_categories
    has_many :categories, through: :sub_categories

class SubCategory
    belongs_to :selected_item
    belongs_to :categories 

class Category
    has_many :sub_categories
    has_many :selected_items, through: :sub_categories

class Customer
    has_many :purchases
selected_items = @category.selected_items

def get_values(customer, selected_items)
    purchases = Purchase.where(customer_id: customer.id)
    selected_items_ids = selected_items.collect { |selected_item| selected_item.id }
    purchased_selected_items_ids = purchases.collect { |purchase| purchase.selected_item_id }
    both_present = selected_items_ids & purchased_selected_items_ids
    # after this how can I send "Not purchased" string for whichever id is not present or send the send the purchased item name wherever ids present in both?

get_values方法中,我需要比较idsSelectedItem之间purchases的{​​{1}}。如果selected_items中任何项目的idselected_item中任何项目中,那么我应该返回purchases,否则返回{{1}的purchased }}不在数据库中,这意味着它将在查询中返回purchase。因此,我必须返回selected_item字符串。这是为每个客户完成的。我怎样才能做到这一点?在这里,我在erb文件的表中显示结果。因此,将为每个selected_item创建列。我应该为每个selected_item传递每个客户是否已购买的值。

3 个答案:

答案 0 :(得分:0)

你可以喜欢,

selected_items  = @category.selected_items

def get_values(customer, selected_items)
    purchases = Purchase.where(customer_id: customer.id)
    result = selected_items & purchases # will give common
    result.nil? ? "not_puchased" : "purchased"
end

答案 1 :(得分:0)

def get_values(customer, selected_items)
  purchases = Purchase.where(customer_id: customer.id)
  selected_items_ids = selected_items.collect { |selected_item| selected_item.id }
  purchased_selected_items_ids = purchases.collect { |purchase| purchase.selected_item_id }
  purchased = selected_items_ids & purchased_selected_items_ids
  not_purchased = (selected_items_ids - purchased_selected_items_ids) + 
                (purchased_selected_items_ids - selected_items_ids)
end

我认为现在您将能够退回已购买的物品,而不是已购买的物品。您可以简单地返回一个数组[purchaseed,not_purchased],其中,sold和not_purchased均为数组并包含各自的ID,或者您可以对其进行迭代并创建一个ID为键的哈希,其值将为item_name(用于购买的ID)/ not_purchased (用于其他ID)并检索哈希。

答案 2 :(得分:0)

最好让SQL处理您要查找已购买和未购买项目的整个情况,例如

  purchase_table = Purchase.arel_table
  selected_items_table = SelectedItems.arel_table
  SelectedItems.joins(:sub_categories).joins(
      purchase_table.join(selected_items_table,Arel::Nodes::OuterJoin).on(
          selected_items_table[:id].eq(purchase_table[:selected_item_id]).and(
            purchase_table[:customer_id].eq(customer.id))
      ).join_sources)
  .where(sub_categories: {category_id: @category.id}).select( 
      "selected_items.*, 
       Case 
         WHEN purchases.id IS NULL Then 'Not Purchased' 
         ELSE 'Purchased' 
       END AS purchasing_status"
   ) 

这应该导致以下结果:

  SELECT 
    selected_items.*, 
    Case 
      WHEN purchases.id IS NULL Then 'Not Purchased' 
      ELSE 'Purchased' 
    END AS purchasing_status
  FROM 
    selected_items
    INNER JOIN sub_categories ON sub_categories.selected_item_id = selected_items.id
    LEFT OUTER JOIN purchases ON purchases.selected_item_id = selected_items.id
       AND purchases.customer_id = [YOU CUSTOMER ID] 
  WHERE 
     sub_categories.category_id = [YOUR CATEGORY ID] 

但是,这是一个非常奇怪的模式,因为我假设客户将做出最终可以购买的选择,并且这些选择属于sub_category。例如

class Customer
  has_many :selected_items
  has_many :purchases, through: :selected_items

class Purchase
  belongs_to :selected_item

class SelectedItem
  belongs_to :customer
  belongs_to :sub_category
  has_one :purchase

class SubCategory
  belongs_to :category
  has_many :selected_items

class Category
  has_many :sub_categories
  has_many :selected_items, through: :sub_categories

在这种模式下,确定SelectedItem的{​​{1}}是否Customer d容易得多