Ruby-on-rails Rails:为循环的每次迭代从表中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/281123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:46:47  来源:igfitidea点击:

Rails: getting data from a table for each iteraction of a loop

ruby-on-railsrubymodel-view-controller

提问by ARemesal

I have a loop (for item in @dataset) and I want, in each iteration, to get different data from another table and make some operations that will be printed in the view. I cant't get this data from the dataset used in the loop.

我有一个循环(用于@dataset 中的项目),我想在每次迭代中从另一个表中获取不同的数据并进行一些将在视图中打印的操作。我无法从循环中使用的数据集中获取此数据。

How can I do this according to MVC? I can put the code into the loop, in the view, but I think it's horrible.

根据MVC我怎么能做到这一点?我可以将代码放入循环中,在视图中,但我认为这很可怕。

Must I use a helper for do this, and call the function from the view?

我是否必须使用助手来执行此操作,并从视图中调用该函数?

Thank you in advance,

先感谢您,

-- ARemesal

-- ARemesal

回答by Orion Edwards

If you have one table, and want to get data from another table, usually this is in the situation of a has_manyrelation. For example, we have @people(Personmodel), and each person has_manyaddresses (Addressmodel). In those cases the best thing to do is this

如果你有一个表,并且想从另一个表中获取数据,通常这是在has_many关系的情况下。例如,我们有@peoplePerson模型),每个人has_many地址(Address模型)。在这些情况下,最好的做法是这样

# Controller
@people = Person.find(:all, :include => :addresses)
...

# View
@people.each do |p|
  p.addresses.each do |address|
    ...

If your data is not just normal database tables (maybe you get it from a web service or so on), then a good thing to do is to build all the data in the controller ahead-of-time, then pass that to the view. Something like this

如果您的数据不仅仅是普通的数据库表(也许您是从 Web 服务等获取的),那么最好提前在控制器中构建所有数据,然后将其传递给视图. 像这样的东西

# Controller
@people = Person.find(:all)
@people.each do |p|
  # attach loaded data to the person object in controller
  p.addresses = Address.load_from_somewhere_by_name(p.name)
...

This way the view code stays clean, like this:

这样视图代码保持干净,如下所示:

# View
@people.each do |p|
  p.addresses.each do |address|
    ...

回答by IDBD

It is quite hard to tell about best solution for your problem without details about data and relations between your models(tables). The common idea is next: keep your views stupid. Get all data needed to render view inside your controllers action. Make all changes and calculations inside same action. Then in view use this data.

如果没有有关模型(表)之间的数据和关系的详细信息,很难说出问题的最佳解决方案。接下来的共同想法是:保持你的观点愚蠢。获取在控制器操作中呈现视图所需的所有数据。在同一操作中进行所有更改和计算。然后在视图中使用这些数据。

btw, if you are talking about N+1 problem then read more about 'include' and 'joins' ActiveRecord parameters.

顺便说一句,如果您在谈论 N+1 问题,那么请阅读有关“包含”和“连接”ActiveRecord 参数的更多信息。

回答by Nadeem Yasin

Person(id, name, other fields...)
Event(id, title, ...)
Date(id, date, time, event_id, ...)
Disponibility(id, percent, date_id, person_id, ...)

Of course, all the relationships are defined in the Model.

当然,所有的关系都是在模型中定义的。

I have a view that shows all the dates for a event, it's easy. But I want, for each date, to print the data for all the people whose are available for that date. In my view, forgetting MVC design pattern, I could code something like this:

我有一个显示事件所有日期的视图,这很容易。但我想,对于每个日期,打印该日期所有可用人员的数据。在我看来,忘记 MVC 设计模式,我可以编写如下代码:

<% for date in @Dates 
  available = Disponibility.find_by_date_id(date.id)
  for item in available
    guy = Person.find_by_id(item.person_id)
%>

Render date and people available...

渲染日期和可用人员...

I want to avoid this in the view. I think the Orion Edwards' answer is the nearest for what I need, but, in that example, address is an empty field for Person table? Or how can I append a new attribute to Person class?

我想在视图中避免这种情况。我认为 Orion Edwards 的答案最接近我所需要的,但是,在那个例子中,地址是 Person 表的一个空字段?或者如何将新属性附加到 Person 类?

Although, am I missing any SQL trick for do this?

虽然,我是否缺少任何 SQL 技巧来执行此操作?

回答by Adam Byrtek

If I was you I would create a separate class that encapsulates the dataset and contains all the logic involved in processing the dataset entries. This class could be iterable (respond to each). Then I would pass an instance of this class to the view and use only its methods there.

如果我是你,我会创建一个单独的类来封装数据集并包含处理数据集条目所涉及的所有逻辑。这个类可以是可迭代的(响应每个)。然后我将这个类的一个实例传递给视图并在那里只使用它的方法。

回答by mwilliams

  • Keep all the logic on your @dataset item inside the controller action
  • Utilize methods in your models for the interaction with other model objects you need
  • You should be left with only @dataset for your view that you can render in a partial.
  • 将@dataset 项上的所有逻辑保留在控制器操作中
  • 利用模型中的方法与您需要的其他模型对象进行交互
  • 您应该只为您的视图留下@dataset,您可以在局部渲染。

You would need to further explain your situation for any more of answer on that. What kind of operations and other models do you need to interact with? If you post your model associations I'm sure we could really square you away.

您需要进一步解释您的情况以获得更多答案。您需要与什么样的操作和其他模型进行交互?如果您发布您的模型关联,我相信我们真的可以将您拒之门外。

Good luck!

祝你好运!