Rails 3:在where方法中使用数组的内容作为变量

时间:2011-05-11 14:31:28

标签: ruby-on-rails ruby arrays block

我有三种模式:Isbn,Sale和Channel。

目标:在isbns show.html.erb视图中获取一个类似于以下内容的列表:

Isbn:myisbn

myisbn的总销售额:100

Myisbn销售渠道1:50

Myisbn对第2频道的销售额:25

通道3:25的Myisbn销售

这是我的模特。 Isbn.rb模型

has_many :sales
has_many :channels, :through => :sales

Sale.rb模型(具有属性sales_channel_id,isbn_id,数量)

has_many :channels
belongs_to :isbn

Channel.rb模型:

belongs_to :sale

我一直在使用show方法中的isbns控制器工作,只是为了让某些东西起作用。我以为我以后会重构 - 关于这些东西是否应该进入模型的建议将是非常受欢迎的。

到目前为止,我已经得到了这个:

@channelisbn = Sale.where("sales_channel_id =?',1).where("isbn_id=?",3)
@channelsalesisbn = 0
@channelisbn.each {|y| @channelsalesisbn =+ y.quantity}

这成功获得了频道ID为1且ISBN id为3的所有销售。但由于ID是硬编码的,因此没有多大用处。所以我把通道ID放到了一个数组中:

@channellist = Channel.all
@channel = 0
@channelarray = @channellist.map {|z| @channel = z.id}

给了我一个可爱的[1,2,3,4]

数组

但我无法弄清楚如何将1,然后是2,然后是3,然后将4传递到一个块,可以用来查找具有该销售渠道ID的ISBN销售。这是我试过的(仍然硬编码ISBN id - 我认为我一次只解决一个问题),它返回一个空数组:

@channelarray.each do |channel|
 @channelisbn = []
 @channelisbn = Sale.where("sales_channel_id = ?", channel).where("isbn_id = ?",3)
 @channelsalesisbn = 0
 @result = []
 @result << @channelisbn.each {|a| @channelsalesisbn =+ a.quantity}
end

然后我要对数组的内容求和。

感谢任何帮助。这是我的第一篇文章,所以我的零接受率很快就会改变!

更新

只是为了完成这个问题,这里是我最终的地方,这很棒,并准备好修补:一个阵列,很好地分组,给我按渠道的isbn销售。感谢group_by小费!

#in the show action in the isbns controller:

@isbn = Isbn.find(params[:id])
@channelarray = Channel.select(:id).all   
@channelarray.group_by {|i| Sale.where("channel_id = ?",i).where("isbn_id =?", @isbn)}

为了清晰起见,在控制台中添加了换行符:

(首先偷偷设置@isbn = 3,因为在控制台中你不能从视图中传递params,所以控制器中定义的@isbn实例在控制台中为nil)

ruby-1.9.2-p180 :067 > @channelarray.group_by {|i| Sale.where("channel_id = ?",i).where("isbn_id =?", @isbn)}

=> {[#<Sale id: 1, isbn_id: 3, quantity: 10000, value: 12000, currency: "GBP", total_quantity: nil, created_at: "2011-05-06 12:30:35", updated_at: "2011-05-07 17:43:13", customer: "Waterstone's", retail_price: nil, discount: nil, invoice_date: "2011-05-24">, #<Sale id: 2, isbn_id: 3, quantity: 1000, value: 500, currency: "GBP", total_quantity: nil, created_at: "2011-05-07 09:37:53", updated_at: "2011-05-07 19:14:52", customer: "Borders", retail_price: nil, discount: nil, invoice_date: "2011-02-05">]=>[#<Channel id: 1>], 

[#<Sale id: 3, isbn_id: 3, quantity: 500, value: 1500, currency: "", total_quantity: nil, created_at: "2011-05-07 09:38:11", updated_at: "2011-05-07 19:15:07", customer: "Borders", retail_price: nil, discount: nil, invoice_date: "2011-12-05">, #<Sale id: 4, isbn_id: 3, quantity: 45, value: 300, currency: "", total_quantity: nil, created_at: "2011-05-07 09:38:38", updated_at: "2011-05-07 19:15:36", customer: "Borders", retail_price: nil, discount: nil, invoice_date: "2011-06-05">]=>[#<Channel id: 2>], 

[]=>[#<Channel id: 3>], 

[]=>[#<Channel id: 4>]} 

更新2

哈,我生成的哈希值的键值对错了。包含销售数据的数组是关键 - 它应该是值。 Rubydocs救了这一天:

@salesbychannel = @salesbychannelwrong.invert

invert方法切换键值对。甜。

1 个答案:

答案 0 :(得分:1)

您正在寻找的是将数组传递给ARel#where(),如下所示:

Sale.where(:sales_channel_id => @channelarray)

这应该执行IN查询。如果这不起作用,您可以随时将数组传递给ActiveRecord #find,如下所示:

Sale.find(@channelarray)

希望这有帮助