Users can create multiple day events.
So an event can have multiple dates, including a start time and end time for the date.
So an event might have these dates:
July 1 2013 9:00am - 5:00pm
July 2 2013 10:00am - 6:00pm
July 7 2013 1:00pm - 5:30pm
So the event table would store the event id, event name, the type of event, address fields for where the event will be held, etc. But how do I model the multiple dates for the event? I could have a separate event_dates table, and that table would have multiple rows for each event:
id event_date event_start_time event_end_time
event1 July 1 2013 9:00am 5:00pm
event1 July 2 2013 10:00am 6:00pm
event1 July 7 2013 1:00pm 5:30pm
But I would also like to query for events occurring on a day (regardless of time), so users could find events in their area that occur on certain days (they might visit multiple events that day).
So how would I model this? Would I use has_and_belongs_to_many, where one event has multiple dates, and one date has multiple events?
In the view, when saving the event, I guess I need to save the event data to the events table, and also save the date/time combination to the other table. But to save the date/time combination to the other table, I need the id for the event that was just saved. So in one form I have a dependency such that the first save must succeed (save event), for the second save to proceed (save date/time using new event id).
Thanks for your help in advance.
I would suggest you set up Event to have many Sessions. Then each session can specify a date/time range for that session.
As far as querying for a particular day goes, you can use a join and a little Rails magic like so: (assuming rails 3.2)
class Event
has_many :sessions
def self.on_day(date)
joins(:sessions).where("session.start < ? AND session.end > ?", date.end_of_day, date.beginning_of_day)
end
have a look at closure_tree gem (https://github.com/mceachen/closure_tree) wich allow you to use acts_as_tree and a hierarchy for your events...
Cheers
Related
I am looking schema for calendar events which includes weekly, monthly and daily events particularly for random days in a week.
In a rails application, I used simple_calendar gem to generate monthly, weekly calendar. Is it possible to configure event creation for say Mon, Wed, Fri between 10-11 AM, if so how should I do that using the simple_calendar gem?
Currently my database schema look like the following,
Schema for calender event
name string
description string
start_date datetime
end_date datetime
duration string
is_scheduled boolean
scheduled_type string (monthly, daily, weekly)
From the README:
Simple Calendar is designed to do one thing really really well: render a calendar.
This is not the right tool to use for creating/tracking/scheduling events. Maybe take a look at something like Montrose or IceCube
In my application I have a provider that has a schedule and clients that book appointment from the schedule. I need the following features.
Provider:
- Be able to specify reoccurring availability. For example Mon 9-5, Tues 10-3, etc.
- Be able to black out datas. For example - not available on this Mon.
- Be able to add single, not reoccurring dates/times slots. For example - This Sat 9-5.
Customer:
- Be able to book single appointments.
- Be able to book reoccurring appointments. (Every Mon 9-4).
So far I came up with 3 options:
Divide the schedule into 30 min intervals and create a database entry for each interval/provider pair. Each interval can be either free or booked. When a customer books an appointment we mark the intervals as booked. The problem with this approach is that it wastes a lot of space, and I am not sure how good the search performance would be for a reoccurring booking.
Save each availability period as an "event". If it is reoccurring, duplicate the event. When searching for free slots search the booking table to make sure that there is no over lapping booking. In this case, searching for reoccurring slots seems a bit awkward. To find all the providers that are available on Mon 9-5 for the next year we will have to search for all the matching 'events' and find all the providers that have 52 matched events.
Save each availability period as an "event". Add a flag if it is reoccurring.When searching for free slots search the booking table to make sure that there is no over lapping booking. It makes it easier to search for reoccurring appointments. To "black out" slot that are suppose to be reoccurring we can just insert a fake booking.
1.Create a event table:
a) With the basic columns eventdate, starttime, endtime, with other details for the event - these are the busy times so are what you block out on the calendar
b) Recurring Events - add columns:
- isrecurring - defaults to 0
- recurrencetype (daily, weekly, monthly)
- recurevery (a count of when the recurrence will occur)
- mon, tue, wed, thur, fri, sat, sun - days of the week for weekly recurrence
- month and dayofmonth - for monthly recurrence
2.The challenge comes when creating the recurring events on the calendar:
- if you create all of them at once (say for the next 6 months), whenever you edit one the others have to be updated
- If you only create an event when the previous one has passed then you need complex logic to display the calendars for future dates
3.You also need rules to take care of whether events are allowed to overlap each other, what resources are to be used, how far ahead the events can be scheduled
My app has users who have seasonal products. When a user selects a product, we allow him to also select the product's season. We accomplish this by letting him select a start date and an end date for each product.
We're using date_select to generate two sets of drop-downs: one for the start date and one for the end date.
Including years doesn't make sense for our model. So we're using the option: discard_year => true
To explain our problem, consider that our products are apples. Vendor X carries apples every year from September to January. Years are irrelevant here, and that's why we're using discard_year => true. However, while the specific years are irrelevant, the relative point in time from the start date to the end date is relevant. This is where our problem arises.
When you use discard_year => true, Rails does set a year in the database, it just doesn't appear in the views. Rails sets all the years to 0001 in our app. Going back to our apple example, this means that the database now thinks the user has selected September 0001 to January 0001. This is a problem for us for a number of reasons.
To solve this, the logic that I need to implement is the following:
If season_start month/day is numerically before season_end month/day, then standard Rails approach is fine.
But, if season_start month/day is numerically AFTER season_end month/day, then I need to dynamically update the database field such that the year for season_end is equal to the year for season_start + 1. For example, if a user selects October 15 to January 15, what he intends is October 15, 0001 to January 15, 0002. I need the app to reflect this.
My best guess is that I would create a custom method that runs as an after_save or after_update in my products model. But I'm not really sure how to do this.
Ideas? Anybody ever had this issue? Thanks!
Add a before_save callback in your Product model.
class Product < ActiveRecord::Base
before_save :fix_end_date
def fix_end_date
self.end_date +=1.year if start_date > end_date
end
end
I am creating a simple todo app where I have 2 types of tasks.
1) regular tasks - These have a due date
2) recurring tasks -These are poped up as reminders on specified date. They can be created either as weekly or monthly reminders. If created for a week, it will be poped up on each week (on a specified date on the week). Likewise for a month it need to be specified the week and the date.
What will be the best way to model this scenario?
I would have two columns for the reminder object - remind_at (date) and repeat_frequency (something to identify different re-occurrences by). That way, you could index the remind_at column and search by it quite quickly. Each time a reminder is shown to user, it would look at repeat_frequency - if it contains directions for repeating, set remind_at to next date, if not, delete/archive the reminder.
You could model a Task to have a due_date. But if a task is recurring, due_date will be null and you would use the recurrence field to compute the next_due_date. recurrence would be a string field holding a parsable string like "tuesday" (for weekly) or "17" (a day number for monthly).
def next_due_date
if due_date
due_date
else
# compute next due date using the 'recurrence' field and today's date
end
end
This may or may not be the "best way" for you, depending on your requirements, and the future needs of the model.
I am working on a Rails app that displays a sports schedule (in a basic table). Each game/event is a db table row. The customer wants all the events to display in chronological order but to also display the time in the event's local timezone.
How can I add a timezone selector to the New action? So that when the customer enters events they can select the event's timezone and input the event's time in that timezone.
Here is what the schedule would look like:
Event 1 7:00 pm EST
Event 2 5:00 pm PST
Event 3 7:00 pm PST
I personally think this is confusing, but it's what they want. Thanks in advance.
Geoff Buesing wrote a great primer on Rails support of time zones back when 2.1 was released.
Okay I am not so sure I understood your question directly but here is my line of thinking there is a library I passed along called tzinfo which is supposed to give Rails the ability to work with timezones so my guess is do the following build a list for a select box (wikipedia has a good complete list) when the user selects the timezone send it to the server which will calculate the appropriate timings now and send back (maybe depending on your architecture you could send only the added hours that should be added or subtracted)