An ActiveRecord is invalid, but has no errors

Did you return false in a before_validation callback?

Distance of time in what you like: days, months, years

Sometimes the Rails helper #distance_of_time_in_words is using too much magic.
When you need a time difference in a specific unit, use this method:
^
def distance_of_time_in(unit, from, to)
diff = to - from

  if 1.respond_to? unit
    distance = diff / 1.send(unit)
    distance.abs.round
  else
    raise ArgumentError, "#{unit.inspect} is not supported as unit"
  end
end

distance_of_time_in(:days, Time.now, 1.year.ago)
=> 365

Remove the .abs if you want the mathematical *differ...

Best GitHub feature

When browsing a repository, pressing "t" allows you to quickly search for file names. Very awesome!

Go here to try it out.

Random list of ActiveSupport goodies

I recently browsed through the ActiveSupport code and found some nice stuff I did not know about:

ActiveSupport::Callbacks
: ActiveRecord-like callbacks, if you need callbacks in non ActiveRecord objects

ActiveSupport::MessageEncryptor
: encrypt and decrypt ruby objects

[ActiveSupport::MessageVerifier](https://github.com/rails/rails/blob/master...

How to make a single check box (or image, etc) align vertically

Consider this HTML:

<div style="line-height: 42px">
  <input type="checkbox" />
</div>

Even though the surrounding container defines a line-height, which vertically centers its inline elements, the check box will be top aligned if it is the only element inside the container.

It will be aligned correctly if the HTML looks like this:

<div style="line-height: 42px">
  <input type="checkbox" /> foo
</div>

Complex explanation here.

So the ac...

Ruby GetText will eval scripts containing ActiveRecord classes

When the Ruby parser module of Ruby-GetText comes across a file in one of its search directories (e.g. lib/scripts/) and finds out that you are defining ActiveRecord classes inside it, it evaluates the whole file. Here is how to avoid that.

What's happening?

Let's say you have the following script which is only run once, manually, via script/runner:

# lib/scripts/doomsday.rb
class User < ActiveRecord::Base; end
User.destroy_all

In that case we ...

Grep the number of occurences in a file, counting multiple hits per line

Grep prints one line per match. To return the number if matches, use the -c switch:

grep -c "something" filename

However, if a word appears more than once in a line, it is only counted once.

To count every match, you can use sed to force line breaks on multiple matches:

sed 's/something/something\n/g' filename | grep -c "something"

Solarized color scheme for Rubymine

If you want to use the (badly implemented!) solarized color scheme in your Rubymine IDE:

  1. Clone the IntelliJ IDEA port:

    git clone https://github.com/jkaving/intellij-colors-solarized.git

  2. Import the settings in Rubymine by chosing the above directory File -> Import Settings

  3. Change the color scheme in the settings (Ctrl-Alt-S) in Editor -> Colors & Fonts

Note that selecting a color scheme will also reset your previous editor font choice. You might want to change the font to [Envy Code R](http://damieng.com/blog/2008/05/2...

Fix: FreeBSD is not able to connect to the network

This article from the FreeBSD Handbook suggests that editing /etc/rc.conf enables DHCP. Unfortunately, some times this seems not sufficient.

Try a reboot, or start the DHCP client manually with the command

dhclient <network adapter>

You can find a list of network adapters by running ifconfig. Examples are em0 or lo0.

How to change the font in Sublime Text 2

Open up your "Base File.sublime-settings" (Preferences Menu → File Settings – User) in Sublime Text 2. Add entries for font_face, and optionally font_size, so it looks similar to this:

{
  "font_face": "Envy Code R",
  "font_size": 11.5
}

Cool: when you save the configuration file, changes will be applied instantly.

Solve issues with Samsung TV as computer screen

We have a big flat screen TV (Samsung LE46c650l1kxxu) in our conference room. Configuring it properly, we were encountering some issues.
This is a list of issues, providing a solution to each.

No sound with DVI/HDMI and separate audio

There are some inputs on the side of the TV. These will not work for split video and audio.
On the back are more inputs you might not have seen. Choose HDMI/DVI as video in and Audio in for HDMI/DVI as audio in. Do not use RCA audio input but the [...

Create RAID on Amazon EC2 EBS volumes

If you use Amazon AWS cloud services you definitively want to utilize software raid for IO intensive stuff such as database data directories and the like.

Here's how you create it:

  • Create some EBS volumes within management console or
  • sudo mdadm --create -f /dev/md0 --chunk=256 --level 10 --raid-devices 4 /dev/sdf /dev/sdg /dev/sdh /dev/sdi
  • Create a fs on /dev/md0 using mkfs*-tools
  • As soon as this is running, you should do that:
    sudo mdadm --detail --scan >> /etc/mdadm/mdadm.conf

Make sure the last line in `/etc/mdadm/md...

Marry Date::Performance and Spreadsheet gems

Date::Performance is a gem that replaces various method in Ruby's Date class with fast C implementations. Unfortunately it doesn't fully implement an internal method (Date.ajd_to_jd) which makes your code blow up when you use it together with the Spreadsheet gem.

A solution is to restore the Ruby implementation of this particular method. To do this, copy the attached file to lib/fix_date_performance.rb to config/initializers.

"The certificate is not trusted because no issuer chain was provided." Error with SSL + Nginx

If you use Nginx with SSL and get an intermediate certificate with your certificate you have to do this:

cat /path/to/intermediate/certificate >> /path/to/certificate

Capistrano: Delete old releases automatically

Whenever you deploy using Capistrano, a new folder is created within the releases directory on the remote server containing the application code.

By default Capistrano 3 keeps the last 5 releases in case you need to rollback to an older release. You can overwrite this setting by using

set :keep_releases, 3

Resque + God + Capistrano

Attached is a working config to deploy an application with Capistrano that needs to monitor Resque workers with God.

God will run as the deployment user, no need to register it as a system service.

Put this into your config/deploy.rb:

namespace :god do
  def god_is_running
    !capture("#{god_command} status >/dev/null 2>/dev/null || echo 'not running'").start_with?('not running')
  end

  def god_command

...

How to: Store multiple Vim commands in macros and recall them

Vim allows recording a batch of commands as a macro. This is handy if you need to do the same things over and over.

Here is how:

  1. Press q to enter macro mode.
  2. Press a letter (not a number!) key to assign a slot to your macro.
  3. You are now recording. Do whatever you want with usual commands.
  4. Once you are done, press q again to stop recording.
  5. You can now run your recorded macro by pressing @ and its assigned letter key.

Cheats:

  • If you want to run a macro repeatedly, type a number before pressing the @ key. Example: ...

Thunderbird: Confirm encryption absence/presence before sending an e-mail

To avoid sending e-mails containing sensitive data unencrypted I strongly suggest you enable a confirmation dialog.

Enigmail can show if the e-mail being sent will be encrypted or not -- and if, which keys will be used. This applies to both clicking the "Send" button or pressing Ctrl+Enter. Thus, you may disable the confirmation for the keyboard shortcut as you get a message box anyways which will avoid sending e-mails by accident.

Instructions

Head over to the Enigmail preferences from the "OpenPGP" menu and tick the corresponding che...

Always store your Paperclip attachments in a separate folder per environment

tl;dr: Always have your attachment path start with :rails_root/storage/#{Rails.env}#{ENV['RAILS_TEST_NUMBER']}/.


The directory where you save your Paperclip attachments should not look like this:

storage/photos/1/...
storage/photos/2/...
storage/photos/3/...
storage/attachments/1/...
storage/attachments/2/...

The problem with this is that multiple environments (at least development and test) will share the same directory structure. This will cause you pain eventually. Files will get overwritten and...