Ruby on rails 通过Koala::GraphCollection进行分页
我正在编写一个Rails函数,它(应该)Ruby on rails 通过Koala::GraphCollection进行分页,ruby-on-rails,ruby-on-rails-3,facebook-graph-api,koala,Ruby On Rails,Ruby On Rails 3,Facebook Graph Api,Koala,我正在编写一个Rails函数,它(应该) 导入所有用户“/me/music” 以50人一组的方式查看每页的详细信息(FB batch API) 我工作得很好。批处理将循环限制在前50个,但我不确定如何使用偏移量重新运行循环。这是我当前的循环: Koala::Facebook::BatchOperation.instance_variable_set(:@identifier, 0) results = @graph.batch do |batch_api| @music.each do |a
Koala::Facebook::BatchOperation.instance_variable_set(:@identifier, 0)
results = @graph.batch do |batch_api|
@music.each do |artist|
if(i == 50)
break
end
batch_api.get_object(artist["id"])
i=i+1
end
end
显然,
@music[0..50]do | artist |
是无效的语法,所以这里没有运气。对于谷歌,这就是我解决问题的方法:
artist_ids = music.each do |artist|
artist["id"]
end
Koala::Facebook::BatchOperation.instance_variable_set(:@identifier, 0)
artist_ids.in_groups_of(50) do |artists|
i=0
results = graph.batch do |batch_api|
for artist in artists do
# ((Your code))
i=i+1
end
end
results.each do |artist|
# ((Your code))
end
end
对于谷歌人来说,这就是我解决问题的方法:
artist_ids = music.each do |artist|
artist["id"]
end
Koala::Facebook::BatchOperation.instance_variable_set(:@identifier, 0)
artist_ids.in_groups_of(50) do |artists|
i=0
results = graph.batch do |batch_api|
for artist in artists do
# ((Your code))
i=i+1
end
end
results.each do |artist|
# ((Your code))
end
end
尽管更好的做法是,如果艺术家/页面/对象列表来自某个集合,则可以在初始集合请求中指定所需的字段,而不是获取ID列表然后逐个工作。尽管更好的做法是,如果艺术家/页面/对象列表来自某个集合,您可以在初始收集请求中指定所需的字段,而不是获取ID列表然后逐个工作。