1

我正在经营一家在线手袋商店,手袋可以有四种颜色——黑色、棕色、橙色和红色。我注意到黑色手袋比棕色手袋卖得更快等等。这意味着人们最喜欢黑色手袋。

在网上商店的主页上,我想选择并以网格布局显示 10 个包。所以我从选择黑色包包开始。如果我的库存中有 10 个或更多黑色袋子,那么我会停下来,不再寻找其他颜色的袋子。但是,如果我有 5 个黑色袋子,那么我会继续寻找棕色袋子。添加那些棕色袋子后,如果我仍然没有 10 个袋子,那么我会寻找橙色袋子等等。

下面是我尝试将解决方案实现为 Rails 模型方法:

class Handbag < ActiveRecord::Base
  belongs_to :store
  attr_accessor :color
end

class Store < ActiveRecord::Base
  has_many :handbags

  def handags_for_display
    selected_handbags = []
    ["black", "brown", "orange", "red"].each do |color|
      bags = get_handbags_by_color(color)
      selected_bags += bags
      if selected_bags.size >= 10
        selected_handbags = selected_handbags[0..9]
        break
      end
    end
    selected_handbags
  end

  private
  def get_handbags_by_color(color)
    handbags.where("color = ?", color).limit(10)
  end
end

虽然这可行,但我很好奇是否有更好的方法来编写它。特别是,我认为这段代码可以转换为使用 Ruby 的枚举器。

4

3 回答 3

0

您应该立即查询数据库,执行以下操作:

@page_offset = ((params[:page].to_i-1)*10) || 0
Handbag.order("color ASC").limit(10).offset(@page_offset)

幸运的是,颜色已经按字母顺序排列。

于 2011-10-21T12:53:42.433 回答
0

你可以试试这样的递归函数。这按预期工作(运行它会给你{:black => 1, :brown => 8, :orange => 1}),你可以改变 get_handbags_by_color 来代替 Rails 工作。

@bags = {
  :black => 1,
  :brown => 8,
  :orange => 10,
  :red => 10
}

@handbag_order = [:black, :brown, :orange, :red]
@max_bags = 10

def get_handbags_by_color(color,limit)
  num = @bags[color]
  num > limit ? limit : num
end

def handbags_for_display(index = 0, total = 0)
  color = @handbag_order[index]
  return {} unless color
  handbags = {color => get_handbags_by_color(color,@max_bags - total)}
  total += handbags.values.inject{|sum,x| sum+x}

  handbags.merge!(handbags_for_display(index+1, total)) unless(total >= @max_bags)
  handbags
end

handbags = handbags_for_display
于 2011-10-21T13:31:58.607 回答
-1
def handags_for_display
     handbags = Array.new
     [{ :color => 'black', :count => 5 }, { :color => 'brown' , :count => 2 }, { :color => 'orange', :count => 1 }, { :color => 'red', :count => 1}].each do |handbag|
          handbags+=Handbag.where("color = ?", handbag[:color]).limit(handbag[:count])
     end
     handbags
end
于 2011-10-21T13:02:07.423 回答