ruby - Ruby ActiveRecord 批量检索新插入的记录?
问题描述
这与我之前的查询有关。
我可以使用以下查询从 ActiveRecord 获取批量记录:
Client.offset(15 * iteration).first(15)
我遇到了用户输入新记录的问题。
假设有 100 条记录,批次为 15。前 6 次迭代(90 条记录)和最后一次迭代(10 条记录)有效。但是,如果有新条目进入(总共有 101 条记录),程序将失败,因为它会:
a) 如果迭代计数器设置为递增,它将查询超出 101 范围的记录,导致不返回任何内容。
b) 如果迭代计数器被修改为仅当整批 15 个项目完成时才递增,则重复最后 14 个项目加上 1 个新项目。
如何获取新发布的记录?
飞镖代码:
_getData() async {
if (!isPerformingRequest) {
setState(() {
isPerformingRequest = true;
});
//_var represents the iteration counter - 0, 1, 2 ...
//fh is a helper method returning records in List<String>
List<String> newEntries = await fh.feed(_var);
//Count number of returned records.
newEntries.forEach((i) => _count++);
if (newEntries.isEmpty) {
..
}
} else {
//if number of records is 10, increment _var to go to the next batch of 10.
if(_count == 10) {
_var++;
_count = 0;
}
//but if count is not 10, stay with the same batch (but previously counted/display records will be shown again)
}
setState(() {
items.addAll(newEntries);
isPerformingRequest = false;
});
}
}
解决方案
推荐阅读
- ios - UIView:如何在视图中圆角子视图的边框?
- single-sign-on - 如何阻止 IE11 缓存安全上下文
- .net - 使用 Noda Time 从本地日期和时区查找最接近的有效全球日期
- java - 当我运行多个测试用例时,TestNG 并行执行总是失败,其中只有一个测试脚本通过
- node.js - Mongoose - 数组被保存为字符串
- php - XAMPP 上没有出现任何帖子
- javascript - Destructuring within object literal with alias
- javascript - Zoom towards are mouse is over
- python - Add a column according to other columns in a matrix. [python]
- r - 将前一个值的一部分与其下一个值累积相加