如何制作ID映射列表

时间:2011-05-23 21:23:42

标签: ruby-on-rails ruby ruby-on-rails-3

我有很长的group_ids列表,如下所示:

 @group_ids = @groups.map(&:group_id)
 Rails.logger.info @group_ids
 [182, 122, 181, 173, 167, 58, 13, 11, 180, 40, 71, 1, 29, 47, 142, 52, 174, 7, 168, 171, 156, 120, 79, 72, 54, 26, 65]

如何获取所有group_ids和输出:

group_id:11 OR group_id:22 etc

2 个答案:

答案 0 :(得分:6)

只是做:

@group_ids * " OR "

(Ruby很棒)

答案 1 :(得分:3)

@group_ids =  [182, 122, 181, 173, 167, 58, 13, 11, 180, 40, 71, 1, 29, 47, 142, 52, 174, 7, 168, 171, 156, 120, 79, 72, 54, 26, 65]
@group_ids.map{|id| "group_id:#{id}"}.join(" OR ")
#=> "group_id:182 OR group_id:122 OR group_id:181 OR group_id:173 OR group_id:167 OR group_id:58 OR group_id:13 OR group_id:11 OR group_id:180 OR group_id:40 OR group_id:71 OR group_id:1 OR group_id:29 OR group_id:47 OR group_id:142 OR group_id:52 OR group_id:174 OR group_id:7 OR group_id:168 OR group_id:171 OR group_id:156 OR group_id:120 OR group_id:79 OR group_id:72 OR group_id:54 OR group_id:26 OR group_id:65"