简体   繁体   English

如何在Rails中对哈希数组的所有值进行排序? 蒙戈

[英]How would I sort all the values of an array of hashes in Rails? Mongo

I have a controller action that grabs some information for me: 我有一个控制器操作可以为我获取一些信息:

@account = Account.find(params[:id])
@acct_cust = Customer.all(:account_id => @account.id.to_s)
@acct_cust.each do |ac|
  @jobAc << Job.where(:customer_id => ac.id).sort(:start_date)
end

We have the account, then we grab all the customers who belong to this account, then we grab all the jobs of each customer and shove them into an array. 我们有一个帐户,然后我们抓住了属于这个帐户的所有客户,然后我们抓住了每个客户的所有工作,并将它们推到一个数组中。

So what I want to do is sort that final array - @jobAc - by the :start_date value. 所以我想做的就是通过:start_date值对最终数组@jobAc进行排序。

Of course I start with plucky queries shoved into this array as you see in the log: 当然,我首先将棘手的查询推入该数组,如您在日志中看到的那样:

Console log: 控制台日志:

 jobAc is [#<MongoMapper::Plugins::Querying::DecoratedPluckyQuery customer_id:  
 BSON::ObjectId('52705ff24031a0000e000179'), sort: [["start_date", 1]], transformer:  
 #<Proc:0x007fe3e6f834d8@/usr/local/rvm/gems/ruby-2.0.0-p247/gems/mongo_mapper-0.13.0.beta2
 /lib/mongo_mapper/plugins/querying.rb:66 (lambda)>>, 
 #<MongoMapper::Plugins::Querying::DecoratedPluckyQuery customer_id: 
 BSON::ObjectId('5279795e2f6984000b00014f'), sort: [["start_date", 1]], transformer: 
 #<Proc:0x007fe3eb13b8f8@/usr/local/rvm/gems/ruby-2.0.0-p247/gems/mongo_mapper-0.13.0.beta2
 /lib/mongo_mapper/plugins/querying.rb:66 (lambda)>>, 
 #<MongoMapper::Plugins::Querying::DecoratedPluckyQuery customer_id: 
 BSON::ObjectId('52797beb2f698400ef0000c4'), sort: [["start_date", 1]], transformer: 
 #<Proc:0x007fe3e9da35e8@/usr/local/rvm/gems/ruby-2.0.0-p247/gems/mongo_mapper-0.13.0.beta2
 /lib/mongo_mapper/plugins/querying.rb:66 (lambda)>>, 
 #<MongoMapper::Plugins::Querying::DecoratedPluckyQuery customer_id: 
 BSON::ObjectId('527dac2d3d126f0107000059'), sort: [["start_date", 1]], transformer: 
 #<Proc:0x007fe3eb170cb0@/usr/local/rvm/gems/ruby-2.0.0-p247/gems/mongo_mapper-0.13.0.beta2
 /lib/mongo_mapper/plugins/querying.rb:66 (lambda)>>]

If I use @jobAc = Job.all(query) I end up with an array of the values like this: 如果我使用@jobAc = Job.all(query) ,则最终得到的值数组如下所示:

[[{1},{2},{3}],[{1},{2}]]

I can't even understand the question I'm trying to ask properly :) Am I trying to flatten an array of hashes? 我什至无法理解我要正确提出的问题:)我是否要弄平散列数组? I tried that but didn't come up with what I thought. 我尝试过,但没有提出我的想法。

I tried : 我试过了 :

@jobAc.sort_by!(&:start_date).reverse but this of course cannot be done while they are plucky queries instead of the actual values.

Any ideas? 有任何想法吗?

The issue here is that you're selecting only parts of your dataset at a time in a loop, rather than sorting the whole dataset. 这里的问题是,您一次循环只选择一部分数据集,而不是对整个数据集进行排序。

You can improve the runtime of this code and apply the sort by performing a single query to find all your jobs, with a sort parameter: 您可以通过执行单个查询以查找所有作业以及sort参数来改善此代码的运行时间并应用排序:

@account = Account.find(params[:id])
@acct_cust = Customer.all(:account_id => @account.id.to_s)
@jobAc = Job.where(:customer_id.in => @acct_cust.map(:id)).sort(:start_date)

Since you're using MongoMapper, I'd recommend that you use associations and scopes: 由于您使用的是MongoMapper,因此建议您使用关联和范围:

class Account
  many :customers
end

class Customer
  belongs_to :account
end

class Job
  scope :for_account, ->(account) do
    where(:customer_id.in => account.customers.fields(:id).all.map(:id))
  end
end

Then you can just find all the jobs for a given customer: 然后,您可以找到给定客户的所有工作:

@jobAc = Job.for_account( Account.find(params[:id]) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM