[Vim] Toggle between Vim and Shell

There is a binding I kind of use a lot in the past months. As you may know, you can switch to a shell from Vim with

1
:sh

in normal mode. If you type exit or hit CTR+D in your shell, you will get back to Vim. I like this behaviour when I am on a remote server via ssh.

Therefor I binded CTR+D in Vim to execute :sh, so I can toggle between shell and Vim with this shortcut.
To get it, include this snippet in your vimrc.

1
2
# ~/.vimrc
noremap <C-d> :sh<cr>

Find all records associated with an ActiveRecord::Base model

If you are too lazy to look up the source file, just hit this up in the rails console.

1
2
3
4
5
InterestingModel.reflect_on_all_associations.map{ |assoc| [assoc.macro, assoc.name] }
# =>[[:belongs_to, :foo_model],
# [:has_one, :bar_model],
# [:has_one, :other_model]
#=> ActiveRecord::Reflection::AssociationReflection

Addtion:
You can also see all associations for an instance of a model.

1
2
InterestingModel.new.reflections
#=> ActiveRecord::Reflection::AssociationReflection

This also works with Classes themself

1
2
InterestingModel.reflections
#=> ActiveRecord::Reflection::AssociationReflection

Note that reflect_on_all_associations returns an Array of all Associations and
reflections will return a Hash like { :association_name => ActiveRecord::Relfection::AssociationReflection }

When struggling with postgresql and utf8/latin

This is just a little information dump for me, you probably won’t have these issues.

Lately I tried to switch the database in a Rails app from SQLite to PostgreSQL.
However, after installing postgres and setting up my database.yml file on my vagrant ubuntu(precise32) box, I fire

1
rake db:create

and it greets me with

1
2
3
4
5
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", 
"database"=>"stuff_development", "pool"=>5, "username"=>"stuffer", "password"=>nil}
PG::Error: ERROR: encoding "UTF8" does not match locale "en_US"
DETAIL: The chosen LC_CTYPE setting requires encoding "LATIN1".
: CREATE DATABASE "stuff_test" ENCODING = 'unicode'

There are some encoding issues, yeah!
After some googeling I tried the solution provided by this post which unfortunatly ended up with this:

1
2
3
postgres=# create database template1 with owner=postgres template=template0 encoding='UTF8';
ERROR: encoding "UTF8" does not match locale "en_US"
DETAIL: The chosen LC_CTYPE setting requires encoding "LATIN1".

But adding some additional options did the trick

1
2
create database template1 with owner postgres encoding='UTF-8' 
lc_collate='en_US.utf8' lc_ctype='en_US.utf8' template template0;

In summary, when you get such errors try these steps

1
2
sudo su postgres
psql
1
2
3
4
5
update pg_database set datistemplate=false where datname='template1';
drop database Template1;
create database template1 with owner=postgres encoding='UTF-8'
lc_collate='en_US.utf8' lc_ctype='en_US.utf8' template template0;
update pg_database set datistemplate=true where datname='template1';