ruby: data structures hash how to programming ruby yaml
by bseanvt
leave a comment
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.
Git: branching errors fixes Git how to invalid workflow
by bseanvt
leave a comment
Git: How to Delete a Branch with an Invalid Name
If you’ve named a branch beginning with two dashes “–”, you’re sort of in trouble because git interprets your branch name as a switch/flag. You can skip switches all together
by supplying two dashes before your branch name
git branch -d -- --index_for_suppliers
and your branch will be deleted!
Set Cron Job to Run Every Five Minutes for a Ruby on Rails Rake Task
First off you’ll need to edit your cron file. Normally, the cron files are kept under /etc/cron.daily or /etc/cron.hourly but we can just use the command line tool, crontab and pass it the -e flag, so that we can edit the file without any fuss.
sudo crontab -e
If this is the first cron you’re installing you should see a blank file or something that looks like this
# m h dom mon dow command
Essentially this is telling you the order of the arguments you need to specify. Each asterisk represents an interval of time. In this order they are
Minute, Hour, Day of Month, Month, Day of Week and then the path to your command. So to set up our cron to run once every five minutes we’ll write
*/5 * * * * /path/to/my/script
It’s important to note that it is */5 and not just 5. The interval for minute is 0 – 59, meaning that if you just enter a 5, you’ll run your cron job only once an hour but at the 5th minute of that hour. To specify that we want to run it every 5 minutes we need the */5. Also, note that the cron will execute your command via the path. If you need an interpreter like ruby, rake or php, don’t forget to put that as a part of your command. For instance this will set a rake task to run every five minutes on a Ubuntu box.
*/5 * * * * cd /var/www/my_ror_application && /usr/bin/rake RAILS_ENV=development db:migrate
You can see that my command uses cd, changing directories to my application. I then specify the full path to the rake program, /usr/bin/rake and give it arguments, including the environment and then of course, the task I want to execute. In this case I’m migrating the database. Which is obviously pointless. Maybe you’ll want to email forum news or send out activation emails.
For more information here is a great reference mkaz.com


