简体   繁体   中英

Creating database objects dynamically based on `datetime`s?

So say I have a TimeSlot object, (which is supposed to represent an interview timeslot), and I have a range of times that I want the timeslots to fill. Say I want to fill timeslots tomorrow from 10AM - 5PM, in 30 minute increments (ie 14 total slots).

A TimeSlot object has two datetime attributes, one that represents the starting time and one that represents the ending time.

I want the administrator of my app to be able to input two datetime 's and an incrementlength (ie 30 minutes), and I want to dynamically generate a collection of TimeSlot objects in my database that represent the specified range of time, cut up into increments of incrementlength minutes. For instance, the TimeSlots generated by the above example would be:

10AM - 10:30AM/10:30AM - 11AM/etc.

Where would I define such a method, how exactly would I create TimeSlot objects within the method, and how would I enable an admin to call such a method? Sorry, I just don't really know where to begin. Thanks.

You whould define that in the model and it will be a class method because its not assigned to one object of the class but to the class itself. I whould solve it like this

def self.create_by_interval(start_time, end_time, intervall)
  ((end_time.to_i-start_time.to_i)/(intervall*60)).floor.times do |factor|
     TimeSlot.create(:start => start_time+(factor*intervall).minutes, :end_time => start_time+((factor+1)*intervall).minutes)
   end
end

When you call to_i on a Date its converted to a unix timestamp so end_time.to_i-start_time.to_i calculates the secounds between start and end date. These seconds are devided by the interval to determine how many objects are needed. THe floor methods rounds a Float down because you cant create 1.99 calendar objects ;)

I wrote this from my head it might not be perfect but it should make clear how it can be done.

// You need to create a form that submits the data needed. Then you can handle the data in the action that receives the entered data in the params? hash. Your form could look like this:

<%= form_for :date_range, :url => working_path_helper do |f| %>
  <%= f-label :start_time %><br />
  <%= f.text_field :start_time %><br />
  <%= f-label :end_time %><br />
  <%= f.text_field :end_time %><br />
  <%= f-label :intervall %><br />
  <%= f.text_field :intervall %><br />
  <%= f.submit %>  
<% end %>

Then in the controller you need to call the class Method like This:

TimeSlot.create_by_interval(Time.parse(params[:date_range][:start_time]), Time.parse(params[:date_range][:end_time]), params[:date_range][:intervall])

The Time.parse method returns a time object basedon a string because you cant transfer anything else then strings over http.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM