Rails create column with a day of week and two date times -
currently building homepage local soccer club , want save training times each team. training times this:
tuesday 18:00-19:30 thursday 18:30-20:00
what best way store these values inside database in rails?
i recommend creating practice model, each practice having :start , :end attribute, each typed :datetime. if generate migration so:
rails generate model practice start_time:datetime finish_time:datetime that build migration database, adding columns need. sure run rake db:migrate update development database.
you have link new model team model. relationship between teams , practices seems one-to-many, you'd add team class
class team < activerecord::base has_many :practices end and add corresponding relationship practice model
class practice < activerecord::base belongs_to :team end (note different use of singular , plural class names in these methods.)
you might build method within practice class render formatted date , time range these 2 attributes.
def practice_time formatted_str = start_time.strftime("%a %h:%m") + " " + finish_time.strftime("%a %h:%m") end you can see more options strftime method here
once have stored date information in logical way, feel free develop model and/or helper methods return data in more useful form. might consider add-ons activeadmin make entering , searching dates easier.
Comments
Post a Comment