Weblog - 9 items for Ruby on Rails
New web server for hosting
Level Systems hosting moves to a new server. The move is to improve speed and flexibility of service for hosting customers.
Instead of using the standard Apache/fastcgi mix to deliver websites we moved to using Litespeed.
Litespeed's built in LSAPI modules for Ruby on Rails is lean and fast doing without the initial page load delay associated with fast-cgi. It also is low on resource use in short giving us the flexibility of using Ruby on Rails without the hit on resources.
The move also gives us more general flexibilty allowing us to streamline our services for the future.
Litespeed, cpanel and Ruby on Rails web hosting
Making Litespeed, cpanel and Ruby on Rails play nicely together in providing a hosting service for many small business web hosting customers was made reasonably easy by Litespeed but still took a bit of tweeking to get right.
The desired end result is to continue using cpanel and WHM as the control panel to manage hosting accounts but to deliver the websites through Litespeed which is a faster and more efficient alternative for hosting many Ruby on Rails sites.
Note: A lot of the information here is found by mixing various info sorced by following links on the Litespeed site.
First step is to follow the instructions on http://www.litespeedtech.com/support/wiki/doku.php?id=litespeed_wiki:apache:cpanel
This takes you through installing Litespeed to first test along side Apache to be sure all the sites work ok on Litespeed and then to replace Apache. The end goal is for Litespeed to be listening on port 80 instead of Apache and to turn Apache off and disable at start up.
The next step is to setup your Ruby on Rails websites on Litespeed, good articles show you how to do this here
http://www.litespeedtech.com/ruby-lsapi-module.html
(follow the easy configuration link for details setup instructions and the use of virtual templates) and a 4 minute demo of how to set up one site here
a 4 minutes demo video by Bob Silva.
I found the easyiest and most streamlined approach was to set up a ruby on rails virtual host template. Using this template it is then easy to deliver new ruby on rails websites. (As easy as filling in 4 fields and click save).
Once I had all our hosting customers ruby on rails websites running ok it was time to address webmail and awstats urls.
Awstats
I enabled awstats in the rails virtual template so all Rails sites would have awstats.
To do this click on "Add ons" set update mode to dynamic or static the other setting as you see fit.
For authentication I created a password file realm called simple access under the security tab and then used that realm for awstats authentication.
When creating the simple_access realm I set the "User DB location" to
$VH_ROOT/config/.htpasswd
that way when I create a new site I create a password file like so for awstats authentication:
htpasswd -c rails/config/.htpasswd myauthname
Webmail
Now to set up a user firendly url to take customers to the horde webmail client at https://mywebsite.com:2096/horde/login.php
Easy enough just set redirect in the rails controller to redirect
/webmail -> :2096/horde/login.php
eg something like this:
redirect_to "https://#{request.host}:2096/horde/login.php"
Cpanel
Likewise if you want a easy to remeber url for you customers to find their cpanel control (which is at :2083 on our server)
Something like this:
redirect_to "https://#{request.host}:2083"
Hope that helps
Rails rss builder for google base, attributes and tags with semi colons
For google base you need to put semi colons in the tag definition.
By default rails doesn't like this.
So you do it like this.
xml.tag!('c:categories', "ski, snow", :type=>"string")
Found solution on this blog about builder
http://blog.katipo.co.nz/?p=29
ActiveRecord Has many through and assignment
With Has many through associaitions if you do a simple assignment of an entire collection it does not work.
eg.
2 models Asset and Client have a has_many through relation ship with each other through AssetClient.
The following does not work
@asset.clients = @clients
So a method is needed in Asset like so to make it work.
def clients=(new_clients)
new_clients.each do |item|
clients << item unless clients.include?(item)
end
clients_to_remove = clients - new_clients
clients_to_remove.each do |item|
clients.delete(item)
end
end
Install exception notifier
run
script/plugin install exception_notification
Configure (put in environment.rb or /config/initializers file)
ExceptionNotifier.exception_recipients = %w(emial@address.net)
ExceptionNotifier.sender_address = %("Application Error" )
ExceptionNotifier.email_prefix = "[ error] "
Put the following in application_controller.rb
include ExceptionNotifiable
Ruby on Rails Making string primary key work for tests
If you want to have primary keys with a type of :string in Ruby on Rails you can do so by using the change_column method after creating a table in the migration.
eg.
create_table "g_languages", :force => true do |t|
t.column "name", :string, :limit => 50, :default => "", :null => false
end
change_column "g_languages", :id, :string, {:limit => 5, :default => "", :null => false}
Problem is if you want to use this Model in your tests the id is autmatically set to :integer and youre string change does not come accross. (The schema.rb is created without any id/primary key info so :integer is assumed)
The fix
Alter the table using ActiveRecord::Migration in test_helper.rb before the fixtures are loaded.
eg.
migrater = ActiveRecord::Migration
migrater.change_column :g_languages, :id, :string, {:limit => 5, :default => "", :null => false}
fixtures :all
Rails 2.1 migrations conversion
If after upgrading to Rails 2.1 your migrations try and go from the first migration you need to populate the schema migrations table with all migrations that have been done.
Use the following sql statement, if you have to many migrations for this a ruby script with loop for the number of migrations should work well.
INSERT INTO schema_migrations VALUES ('1'),('2'),('3'),('4'),('5')
Changing git remote repos with Capistrano - Repository not found
On changing where our remote git repos were stored we got the error
Repository not found
When trying to run
cap deploy
with capistrano.
We had changed the remote origin to the new repos locally with
git remote rm origin
git remote add origin <new remote>
It turned out the you need to change the remote on the server you are deploying to in the folder rails_app/shared/cached-copy
as it still references the old remote. So the fix on the production server was.
cd rails_app/shared/cached-copy
git remote rm origin
git remote add origin <new remote>
Run autospec and features
AUTOFEATURE=true autospec
