Read my latest article: 8 things I look for in a Ruby on Rails app (posted Thu, 06 Jul 2017 16:59:00 GMT)

The legacy of databases with Rails

Posted by Tue, 26 Jul 2005 03:20:00 GMT

As I have had way too much experience with working with horribly ugly and outdated database schemas, I find myself wanting to add new interfaces, but can’t just drop their old schema as other applications rely upon it.

Let’s say that you have an old table that looks like this:

mysql> DESCRIBE client_comment;
| Field               | Type   | Null | Key | Default | Extra          |
| client_comment_id   | int(8) |      | PRI | NULL    | auto_increment |
| client_comment_body | text   | YES  |     | NULL    |                |
2 rows in set (0.00 sec)

In this example, I took the privilege of making the example only two fields, now imagine that this table has over 20-30 fields and they all have client_comment_ in front of the actual string that is of important. Who wants to type all that out over and over? Also, we all refer to this table as the table that holds customer notes, which doesn’t really match what they originally named it. No, we can’t just renmae the table, stuff relies on this structure. But for a new Rails interface, we’re going to tweak our model quickly to interact with the same outdated table in a cleaner fashion.

The first obvious thing is that we need to create a model called customer_note.rb.

class CustomerNote < ActiveRecord::Base
Next, we’ll tell the model that it’s table name is client_comment, rather than customer_notes like it is going to expect it to be. While we are at it, let’s take a quick moment to define the primary key for this table, because id is not what they picked when they first made the table.
class CustomerNote < ActiveRecord::Base

  set_primary_key "client_comment_id" 

  def self.table_name() "client_comment" end

With this, we can already start interacting with our table as if it were called customer_notes.
$ ./script/console
Loading development environment.
>> CustomerNote.create(:client_comment_body => "hello world")
=> #<CustomerNote:0xb7a02cfc @new_record=false, @attributes={"client_comment_body"=>"hello world", "client_comment_id"=>39}, @errors=#<ActiveRecord::Errors:0xb79a4508 @base=#<CustomerNote:0xb7a02cfc ...>, @errors={}>>

Okay, now we want to interact with the field client_comment_body as just body. We want to be able to get and set this value, so we’ll add the following methods to our model.

  def body
    read_attribute "client_comment_body" 

  def body=(value)
    write_attribute "client_comment_body", value

The first method allows you to access (get) the value of client_comment_body with CustomerNote.find(1).body. The second method handles when you (set) the value for body, it then provides the client_comment_body attribute with the value. Technoweenie on IRC helped me get this trimmed down from my original version.

In the end, you have this:

class CustomerNote < ActiveRecord::Base

  set_primary_key "client_comment_id" 

  def self.table_name() "client_comment" end

  def body
    read_attribute "client_comment_body" 

  def body=(value)
    write_attribute "client_comment_body", value


Then, you can do save yourself some typing by accessing the field with the alias that you provided. :-)

$ ./script/console
Loading development environment.
>> a = => "Hello World!")
=> #<CustomerNote:0xb78c6ffc @new_record=true, @attributes={"client_comment_body"=>"Hello World!"}>
=> true
>> a
=> #<CustomerNote:0xb78c6ffc @new_record=false, @attributes={"client_comment_body"=>"Hello World!", "client_comment_id"=>40}, @errors=#<ActiveRecord::Errors:0xb7890d6c @base=#<CustomerNote:0xb78c6ffc ...>, @errors={}>>
>> b = CustomerNote.find(40)
=> #<CustomerNote:0xb788ccd0 @attributes={"client_comment_body"=>"Hello World!", "client_comment_id"=>"40"}>
>> b.body = "Goodbye...cruel... world." 
=> "Goodbye...cruel... world." 
=> true
>> CustomerNote.find(40)
=> #<CustomerNote:0xb7886330 @attributes={"client_comment_body"=>"Goodbye...cruel... world.", "client_comment_id"=>"40"}>
>> CustomerNote.find(40).body
=> "Goodbye...cruel... world." 

Feel free to send me tips if there is an even better way of doing this. :-)

Cheers… and enjoy!

RubyURL This!

Posted by Fri, 22 Jul 2005 22:33:00 GMT

I fixed a bug in the RubyURL This!

A few people reported problems, and I added a catch to prevent those who accidently click on it while on the page. ;-)

(link/instructions on page)

Rails versus J2EE lollipops

Posted by Tue, 12 Jul 2005 22:28:00 GMT

I admit. I never really enjoyed the bitter sweet flavor of the Java lollipop and had the urge to taste it again. I’ve read through a few Struts books and such to get familiar enough with it so that I can assist hosting customers with their configuration issues. Otherwise, I never found myself going out of my way to have the desire to learn it. (I took a Java class about 6 years ago in college… but that was when I decided that I didn’t want to be a programmer).

IBM Developerworks has published this article that compares J2EE and Rails. Is there room for both?

Having been paid to program to work in ASP, ASP.NET (uggh..), Perl, PHP, Python, and now Ruby… I always wonder if I missed out on something by not giving Java another try. I’m not feeling it… I like Ruby for now. Next language? Maybe I will follow in the steps of Lucas and pick up Lisp next… or maybe try Haskell?

What language do you recommend that I dive into next? I want something as sweet as Ruby… is that possible? :-)

Ruby FPDF on Ruby on Rails

Posted by Sat, 04 Jun 2005 13:24:00 GMT

3 comments Latest by sg Mon, 21 Aug 2006 08:57:06 GMT

I have been tinkering with Ruby FPDF for a client all night. I found the examples for it to lack in some real-world examples, so I have taken the example from the RubyOnRails wiki and added a bit more to it. I have added things like an image, links and made a generic letter template. (just an example). I didn’t get into the header/footer functions yet, may do that later.

If you are not sure how to use Ruby FPDF, check out the FPDF documentation.

Just download Ruby FPDF and unpack the archive in your libs directory.

To get Railst to load fpdf, I added this to environment.rb:

).map { |dir| "#{RAILS_ROOT}/#{dir}" }.select { |dir| }
At the top of my controller that uses this lib, I added:
require 'fpdf'

When I browse to this controller/method, I got this PDF to generate with this code.

  def pdf
    send_data gen_pdf, :filename => "robbyonrails-fpdf-test.pdf", :type => "application/pdf"

  def gen_pdf

    d =


    pdf.Cell(0,6, "PLANET ARGON", 0,1,'R')
    pdf.Cell(0,6, "2802 NE 57th Ave",0,1,'R')
    pdf.Cell(0,6, "Portland, OR 97213",0,0,'R')

    pdf.Write(5, "Jane Doe
123 ABC Street
Gilroy, CA 95020


Dear Jane Doe,

I just wanted to say...

Epsum factorial non deposit quid pro quo hic escorol. Olypian quarrels et gorilla congolium sic ad nauseum. Souvlaki ignitus carborundum e pluribus unum. Defacto lingo est igpay atinlay. Marquee selectus non provisio incongruous feline nolo contendre. Gratuitous octopus niacin, sodium glutimate. Quote meon an estimate et non interruptus stadium. Sic tempus fugit esperanto hiccup estrogen. Glorious


Robby Russell
    pdf.Cell(0,6, "PLANET ARGON", 0,1,'L',0,'')

Once again, the output. (click to view PDF)

It’s nothing special, but it’s just an example of how you can add a bit more than ‘Hello World’ to the top of a PDF. I’m still working on figuring out all the x/y stuff. Maybe I will post a better tutorial in the future with headers and footers.

Until then… have fun!

RMagick on Fedora Core 3

Posted by Thu, 02 Jun 2005 15:09:09 GMT

1 comment Latest by Cameron Booth Wed, 12 Jul 2006 03:52:45 GMT

For some reason or another, I couldn’t get rmagick to install with the ImageMagick packages from Fedora Core 3. I tried several different things to get it to install, and it just wasn’t finding some libraries.

So the solution? ImageMagick from source solves the problem just fine… just wish that I had a RPM to work with. I might work on one later.


tar zxvf ImageMagick.tar.gz

cd ImageMagick-6.2.3/

./configure && make && make install

gem install rmagick

In the end…

post-install.rb: installing documentation…
Successfully installed rmagick-1.8.1

Now, to inform the customers who have been waiting on this. :-)

Rails Day, t-minus...

Posted by Wed, 01 Jun 2005 22:26:00 GMT

Casey on IRC asked what some cool gems that you could all use for your Rails Day projects. As I have said before, I will not be participating.. but I am looking forward to playing with the projects that everyone is going to work on. :-)

So, let’s make a list of cool gems that people could use.

gem install rails (obvious) ;-)

What gems do you find to be cool and useful?

Older posts: 1 ... 22 23 24 25