Ruby on rails rails 2和ruby 1.9.2根据不同模型的多个哈希数组进行排序

Ruby on rails rails 2和ruby 1.9.2根据不同模型的多个哈希数组进行排序,ruby-on-rails,arrays,sorting,activerecord,hash,Ruby On Rails,Arrays,Sorting,Activerecord,Hash,我正在对数组对象进行排序,它有来自两个不同模型的多个散列,我需要对另一个模型上具有不同名称的一个fieled进行排序 数组对象如下所示 [#<TechpackAttachment id: 16, company_id: 1, version_id: 38, techpack_identifier: "5bb55062-db53-11e3-abae-d43d7e129fac", body: nil, attachment_description: nil, document_file_na

我正在对数组对象进行排序,它有来自两个不同模型的多个散列,我需要对另一个模型上具有不同名称的一个fieled进行排序

数组对象如下所示

 [#<TechpackAttachment id: 16, company_id: 1, version_id: 38, techpack_identifier: "5bb55062-db53-11e3-abae-d43d7e129fac", body: nil, attachment_description: nil, document_file_name: "sandle.jpg", document_content_type: "image/jpeg", document_file_size: 5717, document_updated_at: "2014-05-22 10:15:36", creator_id: 1, updater_id: 1, created_at: "2014-05-22 10:15:36", updated_at: "2014-05-22 10:15:36", is_primary_image: false>, #<SampleImage id: 13, company_id: 1, vendor_id: 1, sample_id: 4, body: nil, attachment_description: nil, private_to_vendor: 0, image_file_name: "53.png", image_content_type: "image/png", image_file_size: 318585, image_updated_at: "2014-02-19 13:24:05", creator_id: 1, updater_id: 1, created_at: "2014-02-19 13:24:05", updated_at: "2014-02-19 13:24:05", is_primary_image: false>  ] 
给我错误

  undefined method `document_file_name' for #<SampleImage:0xa9cb9298>
未定义的方法“document\u file\u name”#
编辑


我只是举了两个模型为例,但数组对象中有10个模型,其中一些在散列中有文档,另一些在散列中有图像。

您可以检测对象是否响应
document\u file\u name
方法:

@rec = @rec.sort_by do |i|
  if i.respond_to?(:document_file_name)
    i.document_file_name
  else
    i.image_file_name
  end
end

我想这会有帮助的

@rac = [#<TechpackAttachment id: 16, company_id: 1, version_id: 38, techpack_identifier: "5bb55062-db53-11e3-abae-d43d7e129fac", body: nil, attachment_description: nil, document_file_name: "sandle.jpg", document_content_type: "image/jpeg", document_file_size: 5717, document_updated_at: "2014-05-22 10:15:36", creator_id: 1, updater_id: 1, created_at: "2014-05-22 10:15:36", updated_at: "2014-05-22 10:15:36", is_primary_image: false>, #<SampleImage id: 13, company_id: 1, vendor_id: 1, sample_id: 4, body: nil, attachment_description: nil, private_to_vendor: 0, image_file_name: "53.png", image_content_type: "image/png", image_file_size: 318585, image_updated_at: "2014-02-19 13:24:05", creator_id: 1, updater_id: 1, created_at: "2014-02-19 13:24:05", updated_at: "2014-02-19 13:24:05", is_primary_image: false>  ]  

@rec = @rec.sort_by { |i| i.kind_of?(TechpackAttachment) ? i.document_file_name : i.image_file_name }
@rac=[#,#]
@rec=@rec.sort_by{i | i.kind_of?(TechPackageAttachment)?i.document_file_name:i.image_file_name}

是的,我知道,但数组是两个模型的组合。我不明白你的答案(没有代码的部分)。我只是用类名分隔对象。我知道。我理解这部分。我不明白的是“我觉得这很有帮助”这句话。现在好多了。虽然它应该更具描述性,但我认为。
@rac = [#<TechpackAttachment id: 16, company_id: 1, version_id: 38, techpack_identifier: "5bb55062-db53-11e3-abae-d43d7e129fac", body: nil, attachment_description: nil, document_file_name: "sandle.jpg", document_content_type: "image/jpeg", document_file_size: 5717, document_updated_at: "2014-05-22 10:15:36", creator_id: 1, updater_id: 1, created_at: "2014-05-22 10:15:36", updated_at: "2014-05-22 10:15:36", is_primary_image: false>, #<SampleImage id: 13, company_id: 1, vendor_id: 1, sample_id: 4, body: nil, attachment_description: nil, private_to_vendor: 0, image_file_name: "53.png", image_content_type: "image/png", image_file_size: 318585, image_updated_at: "2014-02-19 13:24:05", creator_id: 1, updater_id: 1, created_at: "2014-02-19 13:24:05", updated_at: "2014-02-19 13:24:05", is_primary_image: false>  ]  

@rec = @rec.sort_by { |i| i.kind_of?(TechpackAttachment) ? i.document_file_name : i.image_file_name }