如何有效地检索给定以下模型的数据
这可能是已经被问过的东西,但我找不到它。我有一个包含以下三列的模型:[“id”,“category”,“name”],我想像这样组织我的页面:
标题(所有类别的列表,链接到包含所有类别的表格)该类别的名称以及该类别下的名称数量)
- 类别 1:5 个名称
- 类别 2:7 个名称
- 类别 3:4 个名称
内容(每个类别一个表,列出该类别下的所有名称)
- 表“类别 1”:“name1” 、“……” name5”
- 表“category 2”:“name1”,“...” name7”
- 表“category 3”:“name1”,“...” name4”
要实现此目的,我基本上需要一个类别到名称集合的映射,仅此而已很喜欢,但我想知道最好的做法是什么。首先获取所有类别,然后对每个类别运行一个查询来获取名称似乎效率不高。我在 java 中要做的就是获取结果集中的所有内容,对其进行迭代并构建我提到的地图。不确定如何在 Rails 中实现这一点。
谢谢,
It may be something that has already been asked but I was not able to find it. I have a model that contains the following three columns: [“id”, “category”, “name”] and I would like to organize my page like this:
Header (list of all the categories, link to a table with all the names for that category and count of names under that category)
- Category 1: 5 names
- Category 2: 7 names
- Category 3: 4 names
Content (one table per category listing all the names under that category)
- Table “category 1”: “name1”, “…” name5”
- Table “category 2”: “name1”, “…” name7”
- Table “category 3”: “name1”, “…” name4”
To achieve this I basically need a map of categories to a collection of names, nothing too fancy but I would like to know what the best practice would be. Getting all the categories first and then running one query for each of them to get the names does not seem to be efficient. What I would do in java is get everything in a result set, iterates over it and build the map I mentioned. Not sure about how to achieve this in rails.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)