Ruby 1.9.2 and Rails 3 cannot open rails console

Apparently ubuntu and ruby don’t always catch dependencies like they should. From the first google hit (yeah, I clicked on this stack-overflow in place #2 before checking out the first result.) Navigate to the Ruby source and enter: sudo apt-get install libreadline5-dev cd ext/readline ruby extconf.rb make sudo make install So, if you’re on another … Read more

Rails: Order with nulls last

I’m no expert at SQL, but why not just sort by if something is null first then sort by how you wanted to sort it. Photo.order(‘collection_id IS NULL, collection_id DESC’) # Null’s last Photo.order(‘collection_id IS NOT NULL, collection_id DESC’) # Null’s first If you are only using PostgreSQL, you can also do this Photo.order(‘collection_id DESC … Read more

Specifying column name in a “references” migration

For Rails 5+ Initial Definition: If you are defining your Post model table, you can set references, index and foreign_key in one line: t.references :author, index: true, foreign_key: { to_table: :users } Update Existing: If you are adding references to an existing table, you can do this: add_reference :posts, :author, foreign_key: { to_table: :users } … Read more

Where to put Global variables in Rails 3

If you have already tried restarting your server as Ryan suggested, try putting it in your application.rb like this: module MyAppName class Application < Rails::Application YOUR_GLOBAL_VAR = “test” end end Then you can call it with the namespace in your controllers, views or wherever.. MyAppName::Application::YOUR_GLOBAL_VAR Another alternative would be using something like settingslogic. With settingslogic, … Read more