Ruby on Rails: active record activerecord birthdays cache counter datetime how to scopes timestamp
by bseanvt
leave a comment
Rails Find All by Birthday: How to Find Upcoming Birthdays with ActiveRecord
There are a few ways to solve this problem. However, I think the easiest is to cache the day of the year that the user is born on as an integer. If stored alongside the timestamp we can quickly get a list but properly handle the full birthday elsewhere, such as in the view. You don’t want to rely on just the cached day of year because leap year is not accounted for.
The model will need both born_at and birthday columns.
create_table :users do |t| t.timestamp :born_at # full timestamp t.integer :birthday # just the day of year end
The user model also needs a callback (before_save) to set and or update the cached birthday column based on the full timestamp. For convenience, a named scope can be added to the model which will let you call User.birthdays.
class User
# User.birthdays
scope :birthdays, lambda { where('birthday in(?)', 7.times.map{|i| Time.now.yday + i}) }
def before_save
self.birthday = born_at.yday
end
end
You could also use the week in year (1 – 52) for the cache. Using the day you can look an arbitrary number of days ahead.
Ruby on Rails: 12 hour time datetime helpers initializers leading zero strftime
by bseanvt
5 comments
Rails Helper to Remove Leading Zero in 12 Hour Time Format
I can’t find a strftime() format that will output the hour without the leading zero. For instance 6:20 will be instead 06:20. This just looks a little sloppy. I created a datetime.rb intializer which will contain custom datetime formats for my application.
# RAILS_ROOT/config/initializers/datetime.rb Time::DATE_FORMATS[:date] = "%a %b %d, %Y" Time::DATE_FORMATS[:time] = "%I:%M%p" # ...
Next I set up a helper function which will string together the :date and :time formats and remove the leading zero in the hour with the .gsub method on the string.
# RAILS_ROOT/app/helpers/application_helper.rb
def fulltime(created_at)
created_at.to_s(:date)+" "+created_at.to_s(:time).gsub(/^0/,'').downcase
end
In a view I’ll just pass the timestamp to the fulltime function
fulltime(@post.created_at)
Generate MySQL Datetime Type Using PHP Date() Function
If you want to insert a datetime that matches the default mysql datetime type format use this
date('Y-m-d H:i:s');
Ruby on Rails: datetime helpers time time ago in words
by bseanvt
leave a comment
Date and Time Helpers in Rails
Just for reference http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001695
This post was created about <%= time_ago_in_words @post.created_at %> ago
Ruby on Rails: attr_accessor bug data type datetime validations
by bseanvt
leave a comment
Problem slash Bug in Rails with attr_accessor and Datetime Select Fields
Looks like there is a problem with using the attr_accessor method with datetime form fields http://dev.rubyonrails.org/ticket/8983 In Rails 2.3.2, for me at least, the problem seems to be popping up again. While trying to process a date for credit card validation, I keep getting a ‘nil.klass’ error. Sure enough, remove the datetime select from the form and the problem goes away. I need to look into this a little further because I think that this has been resolved.
More information is available here
http://www.ruby-forum.com/topic/130229
and
http://www.google.com/search?q=1+error(s)+on+assignment+of+multiparameter+attributes&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
The temporary solution is to not use attr_accessor and rather add the field to the database with the correct data type
t.datetime :your_datetime_attribute_you_were_once_virtualizing
. Everything then works as normal. Oh well…


