Rails 3: Automatically create entries in table using preset values - ruby-on-rails

Currently, I'm building a scheduling system for my workplace. It's a highly-trafficked university tour guide office, and given a schedule of the day's events and the shifts of all available tour guides, the system will assign tours to available tour guides.
But, when asking a user to build the event schedule (or list of tours) for the day, I'd prefer to have a schedule auto-generated that they can then manipulate. There are only two different types of schedules that alternate depending on the day of the week, and this head start will save a lot of time.
My question: Where do I put this 'seed' data? Should I create a YAML and then have a method read it to populate a given day with appropriate defaults?
For reference, my models are currently structured like so:
guide has_many :tours, :shifts
shifts belongs_to :guide
tour belongs_to :guide
I've considered a 'day' model but as every time is a datetime, I felt like this was too redundant to persist to the database.
Thanks! All suggestions welcome. I'm just trying to get a good grip on the proper "Rails Way" to handle this.

You should use a factory to initialize your objects. Have a look at https://github.com/thoughtbot/factory_girl

Related

Rails create column with a day of week and two date times

Currently I am building a homepage for my local soccer club and I want to save training times for each team. The training times will look something like this:
Tuesday 18:00-19:30
Thursday 18:30-20:00
What is the best way to store these values inside the database in rails?
I recommend creating a Practice model, with each practice having a :start and :end attribute, each typed as a :datetime. If you generate a migration like so:
rails generate model Practice start_time:datetime finish_time:datetime
That will build a migration for your database, adding the columns you need. Be sure to run rake db:migrate to update your development database.
You would also have to link this new model to your Team model. The relationship between teams and practices seems to be one-to-many, so you'd add this to your Team class
class Team < ActiveRecord::Base
has_many :practices
end
and add the corresponding relationship to your Practice model
class Practice < ActiveRecord::Base
belongs_to :team
end
(Note the different use of singular and plural class names in these methods.)
You might then build a method within your Practice class to render a formatted date and time range from these two attributes.
def practice_time
formatted_str = start_time.strftime("%A %H:%M") + " " + finish_time.strftime("%A %H:%M")
end
You can see more options for the strftime method here
Once you have stored your date information in a logical way, feel free to develop model and/or helper methods to return your data in a more useful form. You might also consider some add-ons like ActiveAdmin to make entering and searching for dates easier.
Here's my solution(may or may not sounds good to you). Create two time columns each represents From and To timings of a day.for example
for Monday it goes like this in your database
|monday_from(time property)|monday_to(time property)|and so on
+--------------------------+------------------------+
Hope this helps you.
Edit: I considered that the training events are repeated in time regularly, so you don't have to stick to a particular date.
You can simply save it to the database as a string.
You can also make a new model (or table) with attributes, start time and end time. Then you can save a Time object to the database.
When loading these Time objects, you can create a helper for view to format the time. It can look the following:
def format_time(time)
time.strftime("%A %H:%M")
end

How best to model reservation/appointments database in Rails

I'm looking to write some sort of Rails app to help users book time slots at a restaurant. How can this be modeled in such a way so that it can be reservations can be displayed and booked through the browser? I have two models:
class Restaurant < ActiveRecord::Base
has_many :reservations
class Reservation < ActiveRecord::Base
belongs_to :restaurant
attr_accessible :name, :date, :time
At first I toyed with a using a hash within the Restaurant model to store availability, using dates as keys. But then I realized that Rails databases must serialize hashes, and I'd like to make sure there isn't a better way to go about this before I dive into that stuff.
I'm using Postgres (if that's relevant). Any help would be much appreciated!
Your basic model structure is fine. Note that attr_accessible is not current best practices, as of Rails 4. (It has been replaced by strong parameters)
Doesn't matter too much about what database you are using (even though PG is a solid choice) but the general engineering that is truly important.
I won't give you a copy paste answer but hopefully give you some direction.
So a Restaurant can have many Reservations throughout the day. I assume each restaurant can only hold so many people and thus have some type of "reservation limit" and Reservations cannot overlap.
Because of the constraints I imagine your 2 model method will work. You just need to figure out exactly how they must interact to work as you plan.
Restaurants should be able to keep track of open times/vacancies (or whatever important details). While Reservations will keep track of the number of people in the party, time, etc.
Your initial relationship looks to be well defined. But the other answer does correctly point out the new Rails 4 preferred method.

Working with different date fields. How to approach towards an easier search engine? Rails 3.2

I built a small site where I gather information of some cultural events happening in the area. I want to extend the Event model so it can handle 3 different 'case scenario' of events.
'i.e. Single'. Normal event with start date and finish date.
'i.e. Band on tour'. Same event but happening at different times.
'i.e. Museum'. Monday to Sunday timetable.
I thought that the best approach for the 1 and 2 types will be:
Event model
# holds title, description, price....
has_many :dates
accepts_nested_attributes_for :dates
Date model
# holds start_time(:datetime) and finish_time(:datetime)
belongs_to :events
I am not sure how to approach the 3rd type. Or what is more important, how to build it in a way that makes future interactions easier to code... like building a SEARCH engine.
Idea:
Building another model called Timetable?
This could hold:
boolean types -> Mon - Tue... -Sun
time type -> Mon_start - Tue_start... - Sun_start - Mon_finish - Tue_finsih... - Sun_finish.
However, this seems pretty complicated to update the search engine(which has a date field/parameter) to iterate through all these 3 types.
Any ideas/experience which may clarify the path to take? Many thanks in advance!
One way that would work well would be to make Date abstract, and then make three more models that extend Date.

How do I create a database record that can repeatedly be copied, but also edited?

This is my first post here, sorry if I can't be specific enough. I'm just starting out with ruby on rails and am trying to build myself a journal app to keep track of workouts. I want to be able to create a workout which contains an indefinite amount of lifts (bench, leg press, etc.) and each of those lifts needs a number of sets, and a number of repetitions. I want the user to be able to add create a workout consisting of say 8 lifts, and then be able to repeatedly copy that to log every day a workout with the sets and reps modified. I know I need a workout model, and then a lift model, I guess I just need help on getting the associations right. Again I can provide more clarity as to what I am trying to do, I don't have that much programming experience, but am figuring it out.
Perhaps you should consider three models:
Lift model - one for each exercise (Bench, Leg Press, Squat). has_and_belongs_to_many :workouts
Workout model - one for each type of workout. has_and_belongs_to_many :lifts.
WorkoutExecution model - one for each time a user does a workout. belongs_to :user and belongs_to :workout.
(You'll of course need a User model, too, if you're allowing users to login and create workouts/ register the execution of a workout).
This way, a user can create a workout in advance (or say, 3 workouts they have in their program), then register the execution of that workout each time they do it by creating an WorkoutExecution record.
This is a fairly complex set of relationships for your first project! Be sure to read the Rails guide on Associations to help get your head around it.

Creating a Calendar/Planner Application - Ruby on Rails

I am considering developing an application using Ruby on Rails that is a planner of sorts. I would like to give a user the ability to see a list of days, click a particular day, and then add things like: Meals, Expenses, Events, Todos, and Exercises. Really I am doing this for me and my growing family.
I am curious with how best to implement this. I can certainly see that Meals, Expenses, etc. need to belong_to :user but I am curious how to implement the belongs_to :day or something like that. Using the created_at or updated_at wouldn't necessarily allow me to provide views for future dates.
I can see how if I created a Days table and then added days through a time and date field that this would work but seems a little strange to ask people to create the actual days.
Or perhaps instead of that I could just create links to variables that search for #today, #tomorrow, but that would get messy.
I have browsed for gems/plugins but can't find one that works. Ideally a person would be able.
Anyone have any thoughts on how to implement something like this?
There are a number of existing Rails calendars, such as http://github.com/elevation/event_calendar or http://github.com/topfunky/calendar%5Fhelper.
However, to answer your specific question about dates: I don't think there's any need to have Day as a model; simply give each event a start date and time, and an end date and time. Remember that Ruby makes it easy to search based on ranges, so "give me all of the events next week" is a cinch if each event has dates and times associated with it.
I'll give it a shot...
Two tables; users and events. A user has many events and an event belongs to a user. Meal, Expenses, etc. are various types of Event. Within events, you can have fields for start and end time of the events. If needed (lets say an events last over multiple days), you could remove the day/time when events occurs into it's own table.
This way, when displaying the calendar for a user, you can find all the events for that date. If none are found, then display nothing.
What do you think?
I would add a model called "Events" and have a properties of the model to represent start date/time, end date/time. I do not think you need a Days model, you can generate your calendar view from the Date class built into ruby.
I have done same kind of project for the Event management in training institute. At there I used event_calender plug in with rails. (enter link description here)
In there we just need to create Event model only. Then we can easily work with that.

Resources