Rails inflection for ‘address’

September 8th, 2008 9:43 pm by Cody Caughlan

On Rails 2.1 (and likely previous versions) I had an inflection that was not generating route names that were acceptable. Given this route:

map.resources :address

It would generate routes like

address GET    /address              {:controller=>"address", :action=>"index"}

new_addres GET    /address/new {:controller=>"address", :action=>"new"}

Notice how the singular form was losing its 2nd ’s’. This is due to how Rails inflects nouns (or I should say model names). Sure, I could have stuck with the missing ’s’ and just dealt with it, but I wanted to fix the actual problem. The answer lies in adding your own custom inflection rules.

For ‘address’ I wanted both the singular and plural form to be the same: ‘address’. Thus I just added a custom inflection in an initializer:

# config/initializers/inflections.rb
Inflector.inflections do |inflect|
   inflect.plural(/^(address)$/i, '\1')
   inflect.singular(/^(address)$/i, '\1')
end

And voila, clean and normal route names.

nginx rewrite rules for WordPress SEO URLs

August 11th, 2008 5:48 pm by Cody Caughlan

I chose to use the SEO-friendly permalink URLs for WordPress, what WP calls “Date and name based”, which means that URLs can look like this:

http://www.ruckusing.com/index.php/2008/03/17/i-love-weddings/

And in order for nginx to properly serve this page some rewrite rules had to be put in place. After much trial and error, these rules finally got it all working.

location / {
   index  index.php index.html;

   #if the file exists, serve it immediately
   if (-f $request_filename) {
      break;
   }

   # SEO urls, allows URLs that look like this
   # http://www.ruckusing.com/index.php/2008/03/17/i-love-weddings/
   if (!-e $request_filename) {
      rewrite  ^/index.php(/.*)$  /index.php?q=$1  last;
      break;
   }
}

Microsoft fails its own SenderID SPF Record Wizard

August 4th, 2008 5:00 pm by Cody Caughlan

I needed to validate our SPF record, Microsoft’s SenderID framework was complaining that our SPF record is invalid. I was using this form on Microsoft.com to check our record.

Running the SPF Query Tool indicated that our SPF records were correct and valid. But still Microsoft thought our record was invalid. So I decided to enter the domain “microsoft.com” into its own tool. What do you think happened?

EPIC FAIL.

Microsoft SPF Fail

I love weddings

March 17th, 2008 2:51 pm by Cody Caughlan

I love weddings. Weddings are a time of happiness, fun and life. Many people from all walks of life coming together to share in the love & happiness of friends or family.

You are pretty much guaranteed excellent times - good food, good drink, dancing, you name it.

Oh yeah, and pretty girls.

Peepin

Ruby: including Comparable messes with nil comparisons

February 13th, 2008 2:09 pm by Cody Caughlan

I had an expression that should have returned a boolean value but instead it was returning nil. The code was:

deleted_at == nil && approved_at != nil

And you would expect to get a boolean value back. However, I was getting nil and tearing my hair out.

It turns out that if your class includes the Comparable module then it over-rides how the comparison operator (==) handles nil values.

As an example, take these two class definitions, one with Comparable and one without.

class Foo
  include Comparable
end

class Bar; end

Now a little console love:

>> f = Foo.new
=> #<foo:0x76a5c>
=> f == nil
=> nil
>> c = Bar.new
=> #<bar:0x7fde9>
>> c == nil
=> false

WTF?

In my case this was inside a Rails ActiveRecord model which had Comparable included.

The solution is to just use the “.nil?” method rather than direct comparisons using the comparison operator to the nil object.

Good explanation of HTTP POST vs PUT

February 12th, 2008 5:17 pm by Cody Caughlan

The exact usage cases of HTTP POST and PUT was a little hazy to me. I found this clear explanation.

HTTP POST vs PUT

Some people, like myself, look for a SQL / database kind of mapping. And for the most part the HTTP verbs can be mapped to SQL operations, but the analogy breaks down when POST and PUT come into play.In a nutshell:

“In SQL analogy, POST is an INSERT with an automatically generated primary key, and PUT is an INSERT that specifies the primary key in the INSERT statement.”

(Thanks to Elliotte Rusty Harold)

Sleazy advertisers and their love of consumers

November 29th, 2007 12:37 am by Cody Caughlan

Ok, this ad was done by MSFT, but its still pretty funny. 

Canadian PSAs are hard-core

November 28th, 2007 4:05 pm by Cody Caughlan

Ouch. 

will_paginate and ruthless execution of a hungry ORDER BY

November 27th, 2007 6:30 pm by Cody Caughlan

I just started using err.the_blog’s will_paginate plugin today and it looks to be quite powerful. And super easy to use. However, I had some arbitrarily complex SQL that I was executing and it was easier for me to use find_by_sql versus ActiveRecord find (and all of its various options).

will_paginate works by taking your query and then running two queries against the DB. One to get the actual results, complete with the appropriate LIMIT values that you feed it. It then runs a second query, a stripped down version, which is necessary for it to get the total number of rows your query would have returned sans the LIMIT values.

The problem was that my ORDER BY clause had some intensive calculations in it and will_paginate was just happily running my stripped down query as-is, with the ORDER BY. That is, will_paginate was not stripping down my query enough. It is silly to run a COUNT(*) query with an ODER BY, its a waste of resources. And especially in my case when my ORDER BY is not pretty.

Fortunately, will_paginate is open source and I was able to patch it up to also strip any ORDER BY nonsense for the COUNT(*) version of the query. The diff is here:

http://blog.ruckusing.com/wp-content/uploads/diff/will_paginate.diff.txt

The regex might be too liberal and end up being greedy, it will probably need to be tweaked. For now, it works.

Abuse of my kindness

November 22nd, 2007 2:17 pm by Cody Caughlan

This is what I get for being a nice guy.

So last Sunday I was letting myself into my apartment when down the hallway I see this woman walking down the stairs. I was not really paying attention but I muttered a “good afternoon” and then went inside. About 10 minutes later there is a knock on my door (the first EVER in my building - everyone keeps to themselves and I never have unexpected guests). I open the door to find the same woman from a few minutes before, and this time I got a good look at her.

Her:
- Very skinny
- TONS of black mascara
- Hair unkempt
- Small bruise on right cheek

She tells me, “I live on the 4th floor and got locked out of my apartment. I had just gone to the store and my housemate was home but had left while I was gone. I dont have my keys….”. Now first off, our building requires a key to enter the outside gate, and I neglected to ask her how she got inside. But I guess she could have ridden someone else’s coattails.

Anyways she asks me, “I really need to use the bathroom, can I use yours?”. The nice guy I am feels sorry for her so of course I acquiesce.

A few minutes later she exits the restroom and then asks if she can come inside, for a few minutes until her housemate returns. Again I say yes.

So she sits down and pulls a pint of gin from her purse. “Its my birthday”, she says, ” and I was out at the liquor store when I got locked out. Want a drink?”. I was very hungover from the night before so I said no. “Oh come on, its my birthday!”. I begrudgingly said ok. Now, I hate gin and she had purchased the cheapest she could find. I got two glasses of ice and she poured a healthy amount in each. I took one sip and was immediately disgusted.

Now, the alarm bells should have been clamoring loudly, already her impression was on shaky ground. But I am such a sucker.

I asked her how she obtained the bruise on her face. “Oh, last night I was mugged. I was knocked unconscious and my attacker was slamming a stick in my back. I was just released from the hospital”. Or some bullshit story like that.

I asked her what she did for a living. “Oh I work in a lab, I am a biologist.” In retrospect, this would have been a perfect opportunity to gauge her truthiness. I dont know that much biology, but I know enough that I could come up with a question to ask her that a layman would likely not know. And if I did not receive an immediate, exact answer then I would know she was full of shit. I could have asked what an enzyme is, or what mitochondria is, or what is the real definition of a calorie. Anything!

But no, I didnt. Somehow the conversation changed to dating websites. “Oh! I have always wanted to do that. Can we make me a profile really quick?”. I was sitting at my desk with my laptop in front of me, so it was not a far-fetched question. Why not. So we hop over to Yahoo Personals and proceed to create a profile for her. She pulls up a seat next to me and off we go. So this is where my attention was not on her 100% as I was asking her questions and then typing them in. In front of me on my desk was my iPod and digital camera. “Oh, you have a Nano! I have never seen one of these before, they’re so cute.” And then picks it up and proceeds to look at my playlists. Tappity-tappity away I go. Then she picks up my digital camera and looks at the photos I had taken that weekend. Tappity-tappity away I go. Oblivious.

Over the course of 20 minutes or so she gets up to go the bathroom. One instance, she leaves the door slightly ajar but I dont hear her going pee like I did the first time. I thought it was kind of strange but didnt dwell on the lack of sound too much.

When we get to her actual “about me” section I press her for details. “I give really good head. I give resume head.”. “What the hell is ‘resume head’?”, I ask. “I give such good head I could put it on my resume.” Oh. Ok. “But no! Dont put that on my profile.”

Over the course of her little visit with Mr. Gullible (me) she has said a number of times that she gives good head. Was she hinting that I should ask her for a blowjob? No way. She was not attractive in the least and I would rather stick toothpicks under my fingernails then have her even touch me. (After she left I had to call a HazMat team to clean my house from her wretchedness.)

The weird thing was that she knew details about my neighbors and my building in general, that she sprinkled into our conversation. Her knowledge of local affairs disarmed me and made me believe that yes, she really did live in the building. She said she lived in 42, on the 4th floor. I believed her.

Now at this point you can see where this is going and the story comes to a close. Eventually she leaves.

With my digital camera and iPod. Which I didnt notice was gone until the next morning.

Upon this revelation I immediately went up to 42. No answer. I went to my managers unit to ask him about 42. No answer.

Its been a few days now and I have not been able to track down my manager (away for the holidays?) and have certainly not found anyone living in 42.

She conned me. She got me good.

I feel so damn stupid. And gullible. And bitter. And angry.

So yes, we did finish her Yahoo Personals profile. So if anyone is looking for an attractive, smart, sassy women to date, by all means, check her out.

No, that is not her real picture.

We had to create a Yahoo email for her, so of course I know her (new) email and password. Not that she is going to ever login, or maybe even remember what it is.

Already she has 71 messages and her profile has been viewed 288 times.

I did the internet dating thing awhile ago and I was lucky if I got a message a week. I am a handsome, smart (oh, wait, maybe not) and good person, I would think I would be a catch. This bitch has men beating down her door.