[英]Rails 3.2 Helpers or Scopes
I'm still wading through Rails, picking up as much as I can. 我仍在趟Rails,尽我所能。
I have the following associations set up: 我建立了以下关联:
Outlet **has_many** Monitorings
Monitoring **belongs_to** Outlets
If I was in a dashboard_controller.rb , and I had the following loop: 如果我在dashboard_controller.rb中 ,则有以下循环:
@revoked_outlet = Outlet.where(:revoked => true)
And then in the view, I'm looping through each outlet. 然后在视图中,我正在遍历每个插座。 However, in the same loop I want to grab the most recent monitoring where :was_revoked => true .
但是,在同一循环中,我想获取最近的监视,其中:was_revoked => true 。
How would I do this according to conventions? 我将如何按照惯例执行此操作?
In your view, try this: 在您看来,请尝试以下操作:
<% @revoked_outlet.each do |outlet| %>
<%= outlet.monitorings.where("was_revoked = true").order(:created_at).last %>
<% end %>
Good luck 祝好运
To follow up on @Anil's answer: 跟进@Anil的答案:
monitorings
, trailing s monitorings
,尾随小号 .where("was_revoked = true").order(:created_at)
a scope on Monitoring
.where("was_revoked = true").order(:created_at)
成为Monitoring
范围
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.