在Rails中创建哈希3

时间:2011-05-22 15:41:25

标签: ruby-on-rails ruby-on-rails-3 hash controller iteration

我有一种方法可以计算ISBN的版税。它按销售渠道分割销售额。特许权使用费率由销售总量确定,特许权使用费通过将特许权使用费率(百分比)乘以总收入来计算。因此,结果应该是每个销售渠道的计算版税数字,每个isbn。原谅可能过度评论的代码。

在isbns_controller的show方法中:

@isbn = Isbn.find(params[:id])

def royalty(isbn)
  @royaltiesbychannel = Hash.new()     
  # Returns all sales for the current isbn, and groups them by channel_id in a hash 
  # {1 => sale 1, sale 2}, {2 => sale 1, sale 2}
  # Then loops through each key-value pair.    
  @isbn.sales.group_by(&:channel_id).each do |ch_id, sale_array|
    # Takes the sales grouped by channel, and totals the quantities and values into two separate variables. 
    value_total_by_channel = sale_array.sum(&:value)  
    quantity_total_by_channel = sale_array.sum(&:quantity)  
     # Gets all the rules for the isbn
     @isbn.rules.each do |rule|
       # Jumps to the next sales hash unless the channel ids match
       next unless rule.channel_id == ch_id
       if Range.new(rule.lower,rule.upper).include?(quantity_total_by_channel)
         @royaltiesbychannel[ch_id] = value_total_by_channel * rule.rate
       end
     end
   end
 end

这是给我一个问题的最后一行。我想要的是生成一个以通道ID作为键的哈希值,并将计算的版税作为值。但相反,它只产生一个键值对的哈希值,如下所示:

{1=>#<BigDecimal:10132a620,'0.875E5',9(36)>}

我怀疑我在方法顶部创建新哈希的方式有问题,并且尝试在方括号中声明密钥。

rule.rate是以小数表示的百分比,例如0.10。该值是一个整数(至少目前)。有关如何生成此哈希的任何想法吗?

其次,在视图中迭代结果哈希的最佳方法是什么?

非常感谢提前。

更新 - 按要求输出.inspect:

{1=>[#<Sale id: 1, isbn_id: 2, quantity: 3000, value: 122000, currency: "", total_quantity: nil, created_at: "2011-05-16 19:52:36", updated_at: "2011-05-22 14:28:33", customer: "WHS", retail_price: nil, discount: nil, channel_id: 1, invoice_date: "2011-02-01", rule_id: nil>, #<Sale id: 2, isbn_id: 2, quantity: 500, value: 3000, currency: "", total_quantity: nil, created_at: "2011-05-19 09:55:00", updated_at: "2011-05-19 09:55:00", customer: "Borders", retail_price: nil, discount: nil, channel_id: 1, invoice_date: nil, rule_id: nil>], 2=>[#<Sale id: 6, isbn_id: 2, quantity: 10, value: 2000, currency: "", total_quantity: nil, created_at: "2011-05-19 10:34:34", updated_at: "2011-05-19 11:21:07", customer: "Bookshop a", retail_price: nil, discount: nil, channel_id: 2, invoice_date: nil, rule_id: nil>, #<Sale id: 7, isbn_id: 2, quantity: 2000, value: 4000, currency: "", total_quantity: nil, created_at: "2011-05-19 10:34:49", updated_at: "2011-05-19 10:34:49", customer: "Bookshop b", retail_price: nil, discount: nil, channel_id: 2, invoice_date: nil, rule_id: nil>]}

第二次更新:对问题的修订:

到目前为止,感谢您的想法。事实证明该方法工作正常,因为@isbn.sales.group_by(&:channel_id)生成的哈希只包含一个键值对。所以我的问题仍然存在,但对于我的代码的不同部分:如何在Sales模型上执行查找以生成像{1 => sale 1, sale 2}, {2 => sale 1, sale 2}这样的哈希数组?

我尝试过这个可怕的事情:

@channelarray = Channel.select(:id).all  
@salesbychannelwrong = @channelarray.group_by {|i| Sale.where("channel_id = ?",i).where("isbn_id =?", @isbn)}   
@salesbychannel = @salesbychannelwrong.invert

给出了这个:

{[#<Channel id: 1>]=>[#<Sale id: 1, isbn_id: 2, quantity: 3000, value: 122000, currency: "", total_quantity: nil, created_at: "2011-05-16 19:52:36", updated_at: "2011-05-22 14:28:33", customer: "WHS", retail_price: nil, discount: nil, channel_id: 1, invoice_date: "2011-02-01", rule_id: nil>, #<Sale id: 2, isbn_id: 2, quantity: 500, value: 3000, currency: "", total_quantity: nil, created_at: "2011-05-19 09:55:00", updated_at: "2011-05-19 09:55:00", customer: "Borders", retail_price: nil, discount: nil, channel_id: 1, invoice_date: nil, rule_id: nil>], [#<Channel id: 2>]=>[#<Sale id: 6, isbn_id: 2, quantity: 10, value: 2000, currency: "", total_quantity: nil, created_at: "2011-05-19 10:34:34", updated_at: "2011-05-19 11:21:07", customer: "Bookshop a", retail_price: nil, discount: nil, channel_id: 2, invoice_date: nil, rule_id: nil>, #<Sale id: 7, isbn_id: 2, quantity: 2000, value: 4000, currency: "", total_quantity: nil, created_at: "2011-05-19 10:34:49", updated_at: "2011-05-19 10:34:49", customer: "Bookshop b", retail_price: nil, discount: nil, channel_id: 2, invoice_date: nil, rule_id: nil>]}

我也试过

Sale.find_by_isbn_id(:id).group_by(&:channel_id)

返回nil

和这个

Sale.find_by_isbn_id(@isbn).group_by(&:channel_id)

返回

undefined method `group_by' for #<Sale:0x0000010122df60>

我有以下型号:

class Sale
  belongs_to :isbn
  belongs_to :channel
  ...
end

class Isbn
  has_many :rules
  has_many :sales
  has_many :channels, :through => :sales
  ...
end

class Channel
  has_many :sales
  has_many :rules
  ...
end

class Rule
  belongs_to :isbn
  belongs_to :channel
  has_many :sales, :through => :isbn
  ...
end

再次感谢。

1 个答案:

答案 0 :(得分:0)

Sale.find_by_isbn_id(@isbn).group_by(&:channel_id) =&gt; Sale.find_all_by_isbn_id(@isbn).group_by(&:channel_id)

Sale.where(:ibn_id => @isbn.id).group_by(&:channel_id)