Rails 3 - 仪表板监控/分析问题

时间:2011-08-12 23:02:16

标签: database ruby-on-rails-3 analytics dashboard

我有以下关联:

Outlet => has_many :problems
User => has_many :problems
Checkin => belongs_to :outlet, belongs_to :user

创建新签到时,会根据当前用户的ID自动设置User_id,并从下拉框(使用outlet_id)中选择Outlet:

<%= f.collection_select :outlet_id, Outlet.all, :id, :name %>

每天需要将每个商店签入一次。

我试图获得两页:

  1. 显示今天尚未签入的所有商店。
  2. 一个显示今天
  3. 的所有奥特莱斯。

    对于第二页我认为我可能会这样做:

    Checkin.where('created_at = Date.today')
    

    但我不相信这是最好的方法吗?或者即使它会起作用? 我不清楚我将如何显示未经检查的奥特莱斯 - 因为我不确定如何获得奥特莱斯列表,并检查每一个是否今天已经签入。

    非常感谢任何帮助。

    更新

    我正在考虑设置它,以便每天为每个插座创建一个签入。但其中一个字段(即:来自[可能是一个布尔])可以取消选中。然后当签入时,它可以勾选复选框。

    但是,这有不得不创造大量记录的缺点。

1 个答案:

答案 0 :(得分:0)

我真的不确定这是一个很好的Rails方式,但这是我最好的尝试:

# All outlet ids into an array
outlets = Outlets.all
outlet_ids = outlets.map(&:id)
# All the outlet ids of the checkins
checked_in = Checkins.where('created_at = Date.today')
checked_in_outlet_ids = checked_in.map(&:outlet_id)
# All outlet ids not in checkins
not_checked_in = outlet_ids - checked_in_outlet_ids

首先,我们抓住所有可能出口的所有ID。然后我们查询找到今天检查过的插座。我们使用Proc技巧来获取两个查询中只有id的数组。然后我们在两个数组上使用差异运算符来返回一个名为not_checked_in的新数组。此数组将包含今天未检查的所有ID。

相关问题