如何在数组中指定条件



如何在Rails中指定每个函数的条件。

@page = 1
@allRecords = #This result will come from API.. It have 500 records.
resultArray = []
@allRecords.each do |t| #need to specify condition here based on @page value
   #processes...
   resultArray << t
end
render :json => resultArray

i将发送@page number给控制器。然后resultArray将发送最终值

Input : @page = 1
Output : resultArray = Records 1 - 50
Input : @page = 2
Output : resultArray = Records 51 - 100
Input : @page = 3
Output : resultArray = Records 101 - 150 
like wise...

我怎样才能做到这一点呢?

任何帮助都会很感激。谢谢。

@page = 1
@allRecords = #This result will come from API.. It have 500 records.
per_page = 50
resultArray = @allRecords.slice(@page + (@page - 1) * per_page, per_page)
# if result is not Array (Enumerable)
#resultArray = @allRecords.to_a.slice(@page + (@page - 1) * per_page, per_page) 

相关内容

  • 没有找到相关文章