Skip to main content

Posts

Showing posts with the label Ruby

Summary of December's Manchester Spring User Group Meeting

The December Spring meeting was held on Tuesday 1st December at the same venue as previous meetings . Two talks were lined up: one on Grails by Adam Evans and one on Spring 3.0 by Rick Evans. Guy Remond of Cake Solutions started the evening off with introductions and explained that the Spring User Group was going to be expanding to become the 'Open Source Central User Group' so as to attract a more diverse audience. He also discussed the Open Source Central website which has plans to become the 'Hub for successful Open Source Enterprise Application Development'. It's going to pull various blogs together into one central place as well as offering video and podcasts of open source technologies. We were also some of the first people to get a physical copy of the Open Source Journal, a 5,000-copy print run (sponsored by Hays IT ). Practical Grails Demonstration by Adam Evans of CTI Adam started his presentation with a brief run through of what is Grails , w...

How to disable the auto-completion 'bell' in Cygwin (using RXVT)

If you have ever hit TAB a few times in bash (via RXVT) you will probably be greeted with the loudest 'bell' your PC can muster. After a while this gets pretty annoying so here's how to disable it if you are using RXVT inside Cygwin (this might work for other Cygwin terminals, I've just not checked) Navigate to your home directory (normally just by typing cd and either edit or create a file called .inputrc Add the following lines to the .inputrc file: # Disable the annoying bell set bell-style none Save the file, close the terminal and reopen - you should now be bell-less! Technorati Tags: Cygwin , RXVT , Andrew Beacock

Pasting code snippets to your blog easily with FormatMySourceCode

When I was preparing my last blog post on disabling HTML elements (in JSPs) with JSTL's "c:if" I needed to change a lot of the JSP markup to prepare it for adding to my posting - the angled opening and closing tags were causing quite a problem. I was about to painstakingly go through changing them all to their 'lt' and 'gt' XML entity equivalents and then remembered a site I bookmarked sometime ago: FormatMySourceCode.blogspot.com It's a very handy webpage with a box to paste your code into, select a few options such as tab width and whether you want an embedded stylesheet and then click "Format Text". It promptly returns with your code (of any kind) converted into a web-friendly version which is perfect for pasting straight into your blog post. It a great yet simple idea by Greg Houston and oh so very useful! Technorati Tags: Greg Houston , Code Formatting , Blogging , Andrew Beacock

How to get the Home & End keys working in Cygwin (RXVT)

Last year I blogged about how to get the Home & End keys working in remote Linux shells & terminals . Well now it's time to sort the Windows shell Cygwin out. If your Home and End keys are producing funny characters on the command line rather than moving the cursor then the input codes of the keyboard need to be remapped. Open your Cygwin terminal and create a file called .inputrc in your home directory, and add the following: # Home Key "\e[7~":beginning-of-line # End Key "\e[8~":end-of-line # Delete Key "\e[3~":delete-char # Insert Key "\e[2~":paste-from-clipboard Now edit or create .bashrc in your home directory and add the following line to ensure the above .inputrc file is picked up: export INPUTRC=$HOME/.inputrc To see if all the above has worked, reopen your cygwin terminal and try it out! If it doesn't work then just delete the .inputrc file and remove the line from .bashrc. Technorati Tags: Keyboard , Mapping , Window...

RXVT - a better console for Cygwin (Unix on Windows)

If you use Windows for software development you will have probably come across Cygwin at some point, the Unix shell for Windows (well a bit of a fake console but it will have to do!). The console out of the box is a bash shell running within a normal Windows cmd prompt box, which is ok but doesn't really give you all the features of a proper Unix shell, click and drag to select text, middle click to paste, decent scroll buffers, etc. However there is a solution to these issues - scrap the silly Windows DOS box and run RXVT instead! First off you will have to run the Cygwin installer again (don't worry it's quite clever and won't corrupt your current version of Cygwin) and select the rxvt terminal package to install. Then continue the installation as normal so that you are left with the Bash shell shortcut icon. You will now need to replace the normal DOS box bash terminal with the new RXVT one. Right-click on the shell shortcut and change the "target" an...

My all new Ruby Database Script Runner - now with Objects!

Back in December I blogged about how to access ActiveRecord & Migrations outside of Ruby on Rails . It was a little to scriptish for my liking so I've refactored it in a couple of classes instead. Before you had to copy the code to a new file and add your database migration inside the Script.run method. I've now broken this up into a DatabaseScript class that extends ActiveRecord's Migration and has the guts of how and where to run any database commands and then any number of script classes that do the actual work. The classes that you write on a day to day basis are now similar in style to the standard Rails Migrations: require 'database_script' class UniqueScriptName def self.run # your migration code goes here end end For example I want to update my_table and set all the updated column for all rows to be true : require 'database_script' class UpdateMyTable def self.run execute('update my_table set updated = true') end end I...

A hint when working with Rails Migrations and legacy databases (ID columns)

Had an interesting problem recently when writing some Rail migrations scripts for an old non-Rails MySQL database. After writing the migration and running it via Rake we found that it was erroring with a complaint about the primary key. We checked that the table had an id column, it did but instead of id it was ID . ActiveRecord is very specific in what it looks for and so was not viewing this ID column as the primary key that it required when adding new rows to the table. A quick lookup in the ActiveRecord documentation pointed out the solution: use set_primary_key in your ActiveRecord class: class SomeLegacyTable set_primary_key :ID end Technorati Tags: Rails , Migrations , ActiveRecord , MySQL , Andrew Beacock

How to access ActiveRecord & Migrations outside of Ruby on Rails

Rails migrations are an extremely handy way of capturing changes to your database and rolling them back if they are incorrect. Manipulating or querying a database is a pretty useful tool and ActiveRecord turns your tables into objects so I looked into how to access these features outside of a Rails environment. I wanted to be able to either specify the database connection details directly in the script if I was running it outside of a Rails application, or to be able to bind into the Rails application's database.yml database configuration file. To run it as a standalone script with the database details described inside the script run: ruby play_with_the_database.rb To run it within the context of a Rails application place the script inside a new directory called db/script and run: ruby play_with_the_database.rb development If you want to run the above script against the production database run: ruby play_with_the_database.rb production I'll show you the ruby code that I came ...

How to rollback Rails database migrations

When you perform database migrations in Rails you use the following command: rake db:migrate If you want to roll back your change to need need to find out what version your database is currently at and then roll back to a previous version: rake db:migrate VERSION=<version to roll back to> I've never liked this way to roll back, seems to much like hard work to me so I found a migration rollback script on the programmingishard site and simplified so that it only rollback by one version: namespace :db do namespace :migrate do desc "Rollback the database schema to the previous version" task :rollback => :environment do previous_version = ActiveRecord::Migrator.current_version.to_i - 1 ActiveRecord::Migrator.migrate("db/migrate/", previous_version) puts "Schema rolled back to previous verison (#{previous_version})." end end end To use this scriptlet, copy and paste the code into a file called db_rollback.rake a...

How to get the schema version number out of your Rails migrations

When you want to revert your Rails migration you need the version number so that you can perform the rollback: rake db:migration VERSION=? One way is to access the database and take a look in the schema_info table to find the version number. An easier way is to add an additional task to Rake (found via the Quoted-Printable ): namespace :db do desc 'Print the current database migration version number' task :version => :environment do puts ActiveRecord::Migrator.current_version end end Copy this code into a file called db_version.rake and place it in the lib/tasks directory within your Rails application. To find out the current version number, simply run: rake db:version Technorati Tags: Rails , Version , Migration , Rake , QuotedPrintable , Andrew Beacock

A Capistrano recipe for restarting Apache 2 (on Linux)

Capistrano is the rather excellent tool for automating Rails application deployment. But it can do a lot more than just uploading Rails applications to live webservers. It's scripts are basically Rake scripts and so have the full power of Ruby behind then so you can pretty much write code to do anything that you want. I wanted a way to reload the configuration for my Apache2 web server running on my home Ubuntu server . I found a post on Mongrel and Capistrano 2.0 by John Ward which showed a very nifty way to create Capistrano tasks for variants of the same base command. My adapted version for controlling Apache is: namespace :apache do [:stop, :start, :restart, :reload].each do |action| desc "#{action.to_s.capitalize} Apache" task action, :roles => :web do invoke_command "/etc/init.d/apache2 #{action.to_s}", :via => run_method end end end Add this code to your config/deploy.rb . This will add four new tasks to Capistrano whi...

How to stop schema.rb from being generated when running Rails Migrations

A couple of days ago I blogged about why rails migrations caused the database schema to be dumped into schema.rb . I've now found a way to stop it from happening without changing the schema_type or modifying any core Rails or Rake classes. What I planned to do was override the db:migrate rake task to do the same as the default one bar the schema dump. My first attempt failed as it appears that rake will allow more than one task to have the same name, it runs then one after the other. The result of this was some strange migrations plus the delay due to dumping the schema! After a little googling I found an excellent post on Matthew Bass's blog entitled Overriding existing Rake tasks . This described a similar situation and I've based my code on his advice. I needed to delete the existing db:migrate task before adding my new one. You can't just call delete task on the Rake::Application class (there's no such method) so Matthew's suggestion was to add a del...

Why do Rails Migrations cause the database schema to be dumped into schema.rb?

Have you ever wondered why rake db:migrate took so long to complete when you ran a simple migration script? The reason is that it's dumping your database's entire schema into db/schema.rb just like rake db:schema:dump does. I had to get to the bottom of why this was happening and searching the internet was not providing any answers, so I took to searching the Rails codebase to see if I could find any clues. databases.rake in /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/ showed the root cause: namespace :db do desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x" task :migrate => :environment do ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil) Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby end If the 'schema_format' is set to':ruby' then run the rake task 'db:schema...

Undefined 'define_a_column' method when using Rail's Oracle OCI adapter?

One of the first hurdles I ran into when I started with Rails migrations was: undefined method define_a_column' for class OCI8::Cursor' It means that your LD_LIBRARY_PATH environment variable does not include the Oracle client binaries. (I got my clue on how to solve this issue from this old defect ticket on the Rails Trac ). Technorati Tags: Ruby , Rails , Oracle , OCI , Andrew Beacock

Some useful Ruby & Rails firefox search plugins

I've found the following two Firefox search plugins useful and thought I'd pass then on: RubyInside This plugin searches a massive list of sites (scroll to the comments for more details) maintained by Peter Cooper, a UK Rails developer who runs the excellent RailsInside website. RailsHelp This plugin was developed by Nick Cody to search the RailsHelp.com website . Update - railshelp.com seems to be down at the moment...

How to change your Rails database adapter from OCI to Oracle

I've started to use Rails migrations recently connecting to an Oracle database. After reading many examples of how to configure the database.yml file on the Rails wiki (as well as ones from Oracle's own website ), I ended up with something like: development: adapter: oci host: database.com/mySID username: schema_name password: schema_name This worked fine, but after researching other migration-related topics I read that oci was the 'old' way to connect to Oracle, the 'new' adapter to use is oracle . A tutorial from Oracle showing usage of the new 'oracle' adapter (scroll down to the section entitled "Part 1: Of configuration, Cursors, and Rows") suggested that your config should look like this: development: adapter: oracle host: DEBIAN username: rails password: rails Note: DEBIAN was the name of their TNS entry specified in their local tnsadmin.ora file. I didn't want to use a reference to a local tnsadmin.ora file on...

Ruby on Rails at June's AgileNorth meetup

Tonight was June's AgileNorth meetup and the topic was Ruby on Rails . It was hosted by David Draper who did an excellent job of introducing Rails in an easy to digest way even though he has very little Rails experience himself. Just shows what a good trainer can do! He demo'ed a shortened version of Curt Hibbs's Recipe Cookbook tutorial using the RadRails plugin for Eclipse as his IDE. David started by creating the database in mysql (note: just the database - no tables) and then used RadRails to create the initial 'cookbook' application environment. He used the ' migration ' feature of rails to create the database tables which were auto-generated by ruby code - this was one feature of rails that I was not aware of and was particularly impressive (for me at least!). He used the scaffolding tools of rails to auto-generate the recipe & category models, the cookbook and category controllers and all the associated views. All of this was done in lit...

Ruby :symbols understood - "A symbol is an object with a name"

Back in August , I blogged about Ruby's symbols as I tried to get my head around what they really were. Jim Weirich made a post yesterday entitled ' Symbols Are Not Immutable Strings ' which really helped solidify my understanding, his main two points being: * Symbols are not immutable strings * A symbol is an object with a name Thanks Jim! Technorati Tags: Ruby , Jim Weirich , Andrew Beacock

Migration to Ruby for Java developers

As an experienced Java developer converting to a Ruby newbie I found the following posts from Sam Newman rather helpful in making the switch: * Ruby For Java (and C#) Programmers, Part 1 - Conventions, methods, modules, and classes * Ruby For Java (and C#) Programmers, Part 2 - Operators, methods, and more on classes * Ruby For Java (and C#) Programmers, Part 3 - Introducing Arrays, Hashes and the typing system If your anything like me having a good book by your side can really help when picking up a new language. There is one Ruby book that everyone seems to have, Programming Ruby: The Pragmatic Programmer's Guide . It really is an excellent book, I liked the first edition so much that I bought the second as well! Let me know if you find any other resources that would be helpful to anyone else making the switch. Another useful resource for people interested in see what Ruby is all about is Try Ruby! - an online interactive Ruby terminal that runs in a browser, created by the...

DevBoi - the Ruby on Rails & web development quick reference sidebar for Firefox

If you use Firefox and develop either websites in HTML/XHTML, CSS & JavaScript or web applications in Ruby on Rails (or PHP) then the DevBoi sidebar written by Martin Cohen is a pretty handy extension to have. Rob Sanheim has a great post introducing the new features and the offline version comes highly recommended if you ever want to develop whilst not connected to the net. Technorati Tags: DevBoi , Firefox , Extension , Development , Programming , Ruby on Rails , Martin Cohen , Rob Sanheim , Andrew Beacock