如何在Rails索引视图中访问复杂数据(如子数据)?(How can I access complex data (like children data) in the Rails index view?)

我正在开发一个Rails应用程序,它将显示食物的营养成分。 我想只显示用户想要看到的营养素。

所以,我有模特:

餐饮: 养分: FoodNutrient:指定每种食物中每种营养素的数量 UserNutrient:指定用户想要看到的营养素我将拥有数千种食物和100多种营养素

我看到几个来源提供了如何处理这种复杂性的提示(现在我正在考虑尝试使用Arel)。 但是,这些来源通常不会提供示例,也不会提示我们应该如何在视图上处理此问题。 我找到了这个,但是会对这个问题有更多的意见,特别是涉及的大数据。

那么,在我的索引视图中处理这个问题的最佳方法是什么?

我有的另一个疑问是,如果使用FoodNutrient模型对性能更好,或者最好在Food模型中包含每个新列代表营养素的列。 我认为FoodNutrient赌注更好,因为用户会选择他会看到哪些营养素,但我不确定。 我将不胜感激任何可能对我有帮助的例子,解释,建议,反馈或参考。

编辑

由于有些人的评论不理解我的问题,我会用其他的方式总结一下。

我想从前3个模型获取数据,最后一个(UserNutrient)我将用来减少向用户显示的行数。

我希望展示如下内容:

Food Name | Nutrient 1 | Nutrient 2 | Nutrient 3 _______________________________________________________ Food 1 10 40 7.3 Food 2 9 4.4 9.1

我明白我会在Food上有一个循环,它会迭代上面显示的每一行。 而且我还必须在第一个循环内迭代UserNutrient以显示每种食物上的营养素数量(此数据在UserNutrient上)。 主要问题是如何进行这些循环,特别是考虑到表格将包含大量数据。 这个似乎有点类似,虽然我不太了解。

我怀疑的是结构是否是最好的结构。 FoodNutrient和Food表可以合并。

I'm developing an Rails app that will display food with its nutrients. I want to show only the nutrients that the user wants to see.

So, I have the models:

Food: Nutrient: FoodNutrient: Specifies the quantity of each nutrient in each food UserNutrient: Specifies which nutrients the user wants to see I will have thousands of foods and more than 100 nutrients

I saw several several sources that give hints on how to deal with this type of complexity (for now I'm considering in trying with Arel). However, these sources usually don't provide examples neither hints on how we should deal with this on the views. I found this one but would love more opinions on the issue, specially concerning the large data involved.

So, how is the best way to deal with this in my index view?

Another doubt that I have is if it is better for performance to have the FoodNutrient model or it is better to include columns on the Food model in which each new column would represent a nutrient. I suppose that the FoodNutrient bet is better as the user will choose which nutrients he will see but I'm not sure. I would appreciate any example, explanation, advice, feedback or reference that may help me.

Edited

As there were some comments from people that didn't understand my question, I will try to summarize it in other words.

I want to get data from the first 3 models, and the last one (UserNutrient) I would use to reduce the number of rows shown to the user.

As I want to show something like:

Food Name | Nutrient 1 | Nutrient 2 | Nutrient 3 _______________________________________________________ Food 1 10 40 7.3 Food 2 9 4.4 9.1

I understand that I would have one loop on Food that would iterate one per row shown above. And I would also have to iterate on UserNutrient inside of the first loop to show the quantity of the nutrient on each food (this data is on UserNutrient). The main question is how to do these loops, specially considering that the tables will have lots of data. This one seems to be a little similar, although I didn't understand well.

My other doubt is if the structure is the best one. The FoodNutrient and Food tables could be merged.

最满意答案

我已经研究过这个问题,现在我决定将FoodNutrient和Food表/模型合并为Food。 我相信有很多行的FoodNutrient会更糟糕,因为它会有一个巨大的索引。 比有很多列的食物表更糟糕。

这篇文章帮我决定:

http://rails-bestpractices.com/posts/58-select-specific-fields-for-performance

如果您有需要添加的内容,请回答问题或添加评论。

I have researched about this problem and for now I'm decided to merge the FoodNutrient and Food tables/models as Food. I believe that a FoodNutrient with lots of rows would be worse as it would have a huge index. Worse than a Food table with lots of columns.

This article helped me to decide:

http://rails-bestpractices.com/posts/58-select-specific-fields-for-performance

If you have something to add, please, answer the question too or add a comment.

如何在Rails索引视图中访问复杂数据(如子数据)?(How can I access complex data (like children data) in the Rails index view?)

我正在开发一个Rails应用程序,它将显示食物的营养成分。 我想只显示用户想要看到的营养素。

所以,我有模特:

餐饮: 养分: FoodNutrient:指定每种食物中每种营养素的数量 UserNutrient:指定用户想要看到的营养素我将拥有数千种食物和100多种营养素

我看到几个来源提供了如何处理这种复杂性的提示(现在我正在考虑尝试使用Arel)。 但是,这些来源通常不会提供示例,也不会提示我们应该如何在视图上处理此问题。 我找到了这个,但是会对这个问题有更多的意见,特别是涉及的大数据。

那么,在我的索引视图中处理这个问题的最佳方法是什么?

我有的另一个疑问是,如果使用FoodNutrient模型对性能更好,或者最好在Food模型中包含每个新列代表营养素的列。 我认为FoodNutrient赌注更好,因为用户会选择他会看到哪些营养素,但我不确定。 我将不胜感激任何可能对我有帮助的例子,解释,建议,反馈或参考。

编辑

由于有些人的评论不理解我的问题,我会用其他的方式总结一下。

我想从前3个模型获取数据,最后一个(UserNutrient)我将用来减少向用户显示的行数。

我希望展示如下内容:

Food Name | Nutrient 1 | Nutrient 2 | Nutrient 3 _______________________________________________________ Food 1 10 40 7.3 Food 2 9 4.4 9.1

我明白我会在Food上有一个循环,它会迭代上面显示的每一行。 而且我还必须在第一个循环内迭代UserNutrient以显示每种食物上的营养素数量(此数据在UserNutrient上)。 主要问题是如何进行这些循环,特别是考虑到表格将包含大量数据。 这个似乎有点类似,虽然我不太了解。

我怀疑的是结构是否是最好的结构。 FoodNutrient和Food表可以合并。

I'm developing an Rails app that will display food with its nutrients. I want to show only the nutrients that the user wants to see.

So, I have the models:

Food: Nutrient: FoodNutrient: Specifies the quantity of each nutrient in each food UserNutrient: Specifies which nutrients the user wants to see I will have thousands of foods and more than 100 nutrients

I saw several several sources that give hints on how to deal with this type of complexity (for now I'm considering in trying with Arel). However, these sources usually don't provide examples neither hints on how we should deal with this on the views. I found this one but would love more opinions on the issue, specially concerning the large data involved.

So, how is the best way to deal with this in my index view?

Another doubt that I have is if it is better for performance to have the FoodNutrient model or it is better to include columns on the Food model in which each new column would represent a nutrient. I suppose that the FoodNutrient bet is better as the user will choose which nutrients he will see but I'm not sure. I would appreciate any example, explanation, advice, feedback or reference that may help me.

Edited

As there were some comments from people that didn't understand my question, I will try to summarize it in other words.

I want to get data from the first 3 models, and the last one (UserNutrient) I would use to reduce the number of rows shown to the user.

As I want to show something like:

Food Name | Nutrient 1 | Nutrient 2 | Nutrient 3 _______________________________________________________ Food 1 10 40 7.3 Food 2 9 4.4 9.1

I understand that I would have one loop on Food that would iterate one per row shown above. And I would also have to iterate on UserNutrient inside of the first loop to show the quantity of the nutrient on each food (this data is on UserNutrient). The main question is how to do these loops, specially considering that the tables will have lots of data. This one seems to be a little similar, although I didn't understand well.

My other doubt is if the structure is the best one. The FoodNutrient and Food tables could be merged.

最满意答案

我已经研究过这个问题,现在我决定将FoodNutrient和Food表/模型合并为Food。 我相信有很多行的FoodNutrient会更糟糕,因为它会有一个巨大的索引。 比有很多列的食物表更糟糕。

这篇文章帮我决定:

http://rails-bestpractices.com/posts/58-select-specific-fields-for-performance

如果您有需要添加的内容,请回答问题或添加评论。

I have researched about this problem and for now I'm decided to merge the FoodNutrient and Food tables/models as Food. I believe that a FoodNutrient with lots of rows would be worse as it would have a huge index. Worse than a Food table with lots of columns.

This article helped me to decide:

http://rails-bestpractices.com/posts/58-select-specific-fields-for-performance

If you have something to add, please, answer the question too or add a comment.