开发者

Rails 3 - Dashboard Monitoring / Analytics Problem

I have the following associations:

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

When creating a New Checkin, the User_id is set automatically from the ID of the current user, and the Outlet is selected from a drop down box (which uses the outlet_id):

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

Each day, each outlet needs to be checked in to once.

Im trying to get two pages:

  1. One to display all the Outlets that have not been checked into today.
  2. One to display all the Outlets that have been checked into today.

For the second page I was thinking I could possibly do:

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

But I'm not convinced this is the best way to do it? or even if it would work? I have no clue as to how I would display the Outlets which have NOT been checked into - as I'm not sure how I would get the Outlet list, and check for each one if a checkin had been made today.

Any help would be greatly appreciated.

UPDATE:

I was considering setting it up so that everyday a checkin is created for each outlet. But one of the fields (i.e.:arrived [which could be a boolea开发者_StackOverflown]) could be unchecked. And then when a checkin is made, it could just tick the checkbox.

But then this has the downside of creating lots of records unncesccessarily.


I'm really not sure there's a great Rails way of doing this, but here's my best try:

# 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

So first we grab all the ids of all possible outlets. Then we query to find the outlets that were checked in today. We use a Proc trick to get an array of just the ids on both queries. We then use the difference operator on both arrays to return a new array called not_checked_in. This array will contain all the ids that aren't checked in today.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜