Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

ruby on rails - Iterating over Sets of ActiveRecord Query Batch

I have a table that has a few thousand sets of 2-3 nearly identical records, that all share a unique "id" (not database ID, but item id). That is, two to three records share the same item id and there are about 2100 records, or ~700 unique items. Example:

{id: 1, product_id:333, is_special:true}, {id:2, product_id:333, is_special:false}, {id:3, product_id:333, is_special:false}, {id:4, product_id:334, is_special:false}...

I'd like to perform a query that lets me iterate over each set, modify/remove duplicate records, then move on to the next set.

This is what I currently have:

task find_averages: :environment do
    responses = Response.all
    chunked_responses = responses.chunk_while { |a,b|  a.result_id == b.result_id}.to_a
    chunked_responses.each do |chunk|
        if chunk.length < 3
            chunk.each do |chunky_response|
                chunky_response.flagged = true
                chunky_response.save
            end
        else
            chunk.each do |chunky_response|
               **manipulate each item in the chunk here**
            end
        end
    end
end

Edit. I worked this one out after discovering the chunk_while method. I am not positive chunk_while is the most efficient method here, but it works well. I am closing this, but anyone else that needs to group records and then iterate over them, this should help.

question from:https://stackoverflow.com/questions/65911915/iterating-over-sets-of-activerecord-query-batch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following code should iterate over an array of items that some of them share common values, and group them by common values:

responses = Responses.all
chunked_responses = responses.chunk_while { |a,b|  a.result_id == b.result_id}.to_a
chunked_responses.each do |chunk|
  chunk.each do |chunky_response|
  **manipulate each item in the chunk here**
  end
end


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...