Archive for the 'ruby on rails' Category

This is Great

Thursday, August 16th, 2007

Behold, the acts_as_enterprisey Rails plugin.

technorati tags:,

Shouldn’t Have Done This in Ruby …

Wednesday, July 18th, 2007
@foo = @bar = [ ]

That causes foo and bar to refer to the same Object, and essentially be the same. After I did it in my code, I wondered why @foo and @bar yielded the same values when I iterated through them in my view.

It’s because I tricked myself.

@foo = [ ]
@bar = [ ]

That works like I was expecting. Should have remembered that…

technorati tags:,

Ruby strftime without leading zeros

Wednesday, January 10th, 2007

Ah. Saved me some thinking time.

technorati tags:,

The Power of method_missing

Sunday, December 3rd, 2006

Ruby’s method_missing (which essentially provides a rescue hook for any calls to methods that are missing - not defined) comes in pretty handy with Rails. (Now, I’m no Ruby on Rails guru, so take the following with a grain of salt, but I found it pretty effective.

Every URL maps to a controller then a view (after your URL, it’s /controller/view). What if a user tries to go to a view that’s not there? Say you have a controller called “foo” that has a view mapped to “bar.” /foo/bar works just fine. But if a user types in /foo/baz, you’re in a little bit of trouble. Here’s where method missing comes in: in your application.rb file, just add a declaration for method_missing and have method_missing take care of things for you by redirecting or otherwise re-routing that bad request. Here’s an example:

def method_missing(url)
  redirect_to :controller => "foo", :action => "bar"
end

Any errant method calls (read: any attempts to visit a controller action that doesn’t exist) is now safely handled: you’ve essentially intercepted that phony URL in application.rb and handled it as gracefully as possible. You can redirect, render a special template, or anything else you’d like. A really, really neat trick!

technorati tags:, , , ,

Overriding Ruby On Rails’s default database naming

Tuesday, November 28th, 2006

Here’s one that took way too long to figure out (mostly because I couldn’t think of a good Google search for what I was looking for). Hope this entry has enough keyword stickiness to help someone else out.

To override ActiveRecord’s default naming schemes (pluralized table names, "id" as the name of the primary key), you can use the following ActiveRecord methods:

  set_table_name "MY_TABLE_NAME"
  set_primary_key "MY_KEY_NAME"


Those methods should override Ruby on Rails’s default behaviors and let you use your own names for ID/primary key fields and table names.

technorati tags:, , , ,

File upload content types on Ruby on Rails one-liner

Sunday, November 12th, 2006

Need to save the content_type of a file upload in Ruby on Rails? Here’s a simple one-liner for your model that does it:

self.content_type = incoming_file.content_type.to_s.strip!

where incoming_file is a reference to the file that’s been uploaded. Why the strip! call? Because Rails (at least on Windows) stores an extra character (newline / carriage return) at the end of the content_type field. strip! (the bang method) drops that last character in place, letting you do everything in that single line.

technorati tags:, , ,

A Big Plug for RadRails

Friday, October 27th, 2006

Yesterday at work, we held a Hack Day (for those unfamiliar, you get 24 hours to build a prototype on just about anything you want). My team chose to build a company intranet in Ruby on Rails. Why Rails? Because it’s fast and easy and is my language + framework of choice for web development. Gregg, one of our top Java guys and my teammate, suggested we use RadRails as our IDE. I’d downloaded it at home but never had a chance to test it out. Plus, I’m not usually a big Eclipse fan (RadRails is based on Eclipse). When the day was done, I was very impressed with what RadRails did.

RadRails really works as the center of your development environment, providing things like a server and quick access to generators to get development moving. It makes already fast things (like script/generate tasks) even faster (no need to leave the environment and jump to a terminal to run that script). Bottom line: RadRails streamlines your Ruby on Rails development workflow. Go get it!

technorati tags:, , ,

Blogged with Flock

Ruby on Rails: Selecting the last item in a database table

Thursday, October 26th, 2006

Ruby on Rails has the great find(:first) method that lets you grab the very first ActiveRecord object out of a given database table. That’s all well and good, but what if you want the last? Use this: Model.find(:first, :order => “id DESC”). That will give you the first record in a reversed list of IDs: basically, the record with the highest ID value, or the last record in the table.

Not good enough for you? Order by any other field. Want a different first record ? Sort by a field ASC. The :first record really depends on what order your records are in.

technorati tags:, , Ruby, Rails, ActiveRecord

RoR - Complete forum in under 500 lines of code

Friday, September 1st, 2006

Beast: An open source Rails forum in under 500 lines of code. Yeah, that headline caught my eye, too. The thing is, I checked out the demo, and it looks great and is robust. They’ve definitely captured the everything you expect from a forum. Even if it was 10,000 lines of code, it’s great software; the "brevity" of Beast just makes it that much better. It’s open source, and is definitely worth a look if you’re rolling on Rails.

technorati tags:, , ,

Blogged with Flock

Nice little Ruby on Rails random password generator

Sunday, August 20th, 2006

Spits out a nice little alphanumeric password. Great for a random password generated on signup. Only a two-line function, to boot.

I'm Reading…
Search This Site
You are currently browsing the archives for the ruby on rails category.

AddThis Feed Button

Need great hosting?

Categories