Rails migrations: creating a MySQL BIGINT
add_column :facebook_user_id, :integer, :limit => 8 # BIGINT
add_column :facebook_user_id, :integer, :limit => 8 # BIGINT
rake TEST=test/units/foo_test.rb
Source: http://rails.lighthouseapp.com/projects/8994/tickets/168-patch-generators-create-tests-with-require-test_helper-which-doesn-t-work
You can do this:
animals.collect(&:name)
…to return an array of animal names. This is essentially a shorthand version of:
animals.collect { |animal| animal.name }
Add this to the model:
# prevent ActiveRecord stumbling on the column named 'hash'
# by removing it from the column list
class << self # Class methods
alias :all_columns :columns
def columns
all_columns.reject {|c| c.name == 'hash'}
end
end
Solution half-inched from: http://groups.google.com/group/compositekeys/msg/202d04713ecbd6ee
Cleaner than checking if the value of fruit.id is nil:
fruit.new_record?
if request.format.html?
# do stuff
end
Works for any valid MIME type…
if request.format.xml?
More details at http://ryandaigle.com/articles/2007/2/15/what-s-new-in-edge-rails-mime-type-convenience-methods
@controller.controller_name
RAILS_DEFAULT_LOGGER.error('log message')
…will write to the log file of the current environment.
When you make changes to your model objects, you can reload them in the console using:
reload!
I set up Mongrel on a server using Plesk 7. Each Rails app has a single Mongrel webserver1 running on the local interface (port 8001 and up). The Mongrel webservers are not accessible to the outside world. They are proxied through Apache using mod_proxy. Just add this snippet to the vhost.conf for the appropriate domain:
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://0.0.0.0:8001/
ProxyPassReverse / http://0.0.0.0:8001/
Replace 8001 with the chosen port for your app. (The block does not need to be inside a VirtualHost or Directory container.) Remember to rebuild the Plesk configs afterwards with:
/usr/local/psa/admin/sbin/websrvmng -a -v
You’ll now need to add a Mongrel webserver for the app in /etc/init.d/mongrel:
apps = {
'firstapp'=> 8001,
'secondapp' => 8002
}
Enter the app name and your chosen port number into the ‘apps’ hash, and save the file.
capify .
This will generate some Capistrano files in your working copy folder, including a generic deployment recipe (deploy.rb). We need to customise deploy.rb to work with our setup. If the SVN repository isn’t accessible from the live server, you’ll also need to install a Capistrano module to enable use of a local Subversion repository - download from here:
Capistrano module for local SVN access
The zip file contains two .rb files that you’ll need to put into your app’s lib/tasks directory.
Make sure you add and check in all of the Capistrano files to Subversion when you’re done.
Now we’re ready to deploy the app for the first time. In your working copy directory, do:
cap deploy:setup
cap deploy
This will export the latest copy of your project from Subversion, upload it to the server, and start Mongrel.
To update the version of your app on the server, use:
cap deploy
If you have any problems with your release, you can easily roll back to the previous version with:
cap deploy:rollback
RAILS_ENV=production rake db_schema_import
The RAILS_ENV=production part ensures that the production database details are used by rake.
If your database is already on the live server and you need to run migrations against it, you can do:
RAILS_ENV=production rake db:migrate
Or from kermit:
cap deploy:migrations
# ls -l /u/apps/firstapp/releases/
drwxr-xr-x 13 root root 4096 Dec 18 14:48 20061218144553
drwxr-xr-x 13 root root 4096 Dec 18 15:23 20061218152030
drwxr-xr-x 13 root root 4096 Dec 18 15:54 20061218155131
The most recent release is symlinked to /u/apps/APPNAME/current. This is where Mongrel serves the application from.
Some files (including log files) are shared between releases, so you don’t lose them when you deploy a new version. These live in the /u/apps/APPNAME/shared folder.
If you’re concerned about the releases folder getting too full, you can delete all but the most recent 5 releases with:
cap deploy:cleanup
1. I had intended to set up Rails using a Mongrel cluster but you need Apache 2.1 or higher to use mod_proxy_balancer.
2. Plesk password protection will not work. All requests to the domain are proxied to Mongrel, so Apache auth is ignored.