Category Archives: rails

Rails / Ruby DateTime Stuff

A great blog entry: http://anandmuranal.wordpress.com/2007/12/12/formating-date-time-in-rails/

Rails update a gem to an older version

You want say rack version 1.0.1 but the latest version is rack 1.1 and it’s installed on your machine:

gem install [gem name] –version [version number]

For example:
gem install rack –version 1.0.1

Getting Threads in Rails

So you want threads in rails? Add the following 2 lines to your production.rb file:

config.threadsafe!
config.eager_load_paths << “#{RAILS_ROOT}/lib”

For more reading, checkout: http://m.onkey.org/2008/10/23/thread-safety-for-your-rails

Rails: Unpacking your gems into vendor/gems

Okay you have your marvelous gem but want to put it into the vendor directly.

No worries. In this example I will be working with the “marc (0.3.0)” gem.

  1. Edit the environment.rb file and under the “Rails::Initializer.run do |config|” put the following line: config.gem ‘marc’
  2. Next “rake gems:unpack GEM=marc” where GEM=gem_name – don’t worry about the version number.

All done. You should have a new directory under vendor/gems/gem_name.

Troubleshooting. If this doesn’t quite work, you might want to try:

  • Create the gems directory under vendor yourself
  • It may complain that it lacks the specification file. If so go to the vendor/gems/gem_name directory and run: gem specification gem_name > .specification

If this still doesn’t work … then google or bing yourself to death.

Metaprogramming in Ruby is awesome

Well I had to override the behaviour of the Oracle Enhanced Adapter when creating sequence names. By default it creates them as “tablename_seq” where our database was “sq_tablename”.

After reading this post. I made my change and placed it under the config/initializers directory.

But the concept of metaprogramming in Ruby is really intriguing.

Netbeans Ruby Oracle OCI Mac Problem

Okay. You are using netbeans and connecting to Oracle. You have installed the Oracle Instant Client and have set the DYLD_LIBRARY_PATH=’/instantclient’ in your shell. All is well if you run your rails app from the command line.

Right.

But when you run it through Netbeans you get the error telling you to some effect:

“ERROR: ActiveRecord oracle_enhanced adapter could not load ruby-oci8 library. Please install ruby-oci8 library or gem.”

The explanation is that for some reason Netbeans on the mac does not pick up the settings set say in your .bashrc file. The easiest solution is to add the line:

“source ~/.bashrc” to your netbeans startup script.

Now Netbeans will pick up all your paths.

Excuting Rails Unit Tests without blowing away your database

Okay …

I have my nice shiny rails app. I have written a heap of tests. But I don’t want to blow the TEST database away each time I execute rake test:units.

Be aware when you use say “rake test” it calls “rake db:test:prepare” which essentially blows away your test database and reloads it.

There are a couple of approaches:

One: Run the test through Ruby and not Rake
——————————————-
Run your test: ruby -I test test/unit/my_test.rb

The -I flag ensures that the file /test/test_helper.rb is loaded.

You could then write your own rake task to run all the unit tests.

Two: Write your own rake task to use instead of rake test:units
—————————————————————
In this approach. The aim is to ensure that the “db:test:prepare” task is not called. You do this by clearing the prerequisites for “rake test:units”

i.e.
task :test_units do
Rake::Task[‘test:units’].prerequisites.clear
Rake::Task[‘test:units’].invoke
end

You then invoke your task: “rake test_units”
Also call “rake -T” to see your task listed.

For more details:
Check out Paul Barry’s post
Jay Field’s post

Rails 2.0 ActionMailer and SMTP email

Okay, you want to use ActionaMailer in Rails to send smtp mail. First some useful links:


1 – Create a new file in the config/initializers/action_mailer.rb (The file can be called anything)

2 – Place the following settings in it:



ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.raise_delivery_errors = true

ActionMailer::Base.smtp_settings = {
:address => "your.smtp.server",
:port => 25,
:domain => "your.domain",
:user_name => "username",
:password => "password"
}

You should be able to send SMTP mail. Go and read the above doc about using ActionMailer to create the email and send it.

On the Rails Hype Cycle

I found this interesting graph on a blog entry about the Technology Hype Cycle.

The diagram is interesting and made me think about my current Rails experience. Each time I code and do something, I really enjoy myself. A side question is … am I allowed to enjoy myself whilst coding? Past experience in most Java frameworks has been defined more by pain than “enjoyment”. I digress.

Two days ago a new rails book I ordered from Amazon arrived(Simply Rails 2 – by Patrick Lenz). I was frothing with anticipation. Even though I have only read the first few chapters and I haven’t really learned anything that I didn’t already know, yet I am still frothing.

I think that I am still on the way to reach the peak of Inflated Expectations. I just hope that when the Trough of Disillusionment sets in that it won’t be so bad. I guess live it up now.

I am really enjoying Rails!

Well for a freelance project, I decided to use Ruby On Rails. I have heard the hype. Seen developers at work use it … blah etc.

After mixing tutorials for Rails 1.2 and Rails 2.0 (There are not that many for 2.0) I actually managed to code something quite easily. I was quite surprised. I think that the scaffolding thing is just awesome. I even “rewrote” my small app a few times as I learnt to do it better. I think the telling thing was that I wrote the app in Rails 2.1.0. Once I uploaded it to my server I was having it hosted on, I realised that they only offered Rails 2.0.2. I rewrote it again. I even managed to figure out how to various rails versions on my local machine and use the version I wanted. Very cool.

Some helpful Rails 2.0 tutorials:

I did this all on my mac. I installed Mamp to get MySql and I eventually settled on textmate as editor as my machine just did not have enough grunt to use Eclipse.

The really great thing is that I am enjoying it. There are some things that have been frustrating, but this has been way easier to learn than say Spring/Hibernate combo.