Posts Tagged ‘Geek’

Extend has_many with the :extend option

Wednesday, December 9th, 2009

The method has_many has several options that help you refine an association. The :extend option is one that I found myself using recently, and thought it would be nice to share with the internet.

Say you have a Friendship class that belongs to a User class, and that your User class has_many :friends, :through => :friendships. A good old fashioned @user.friends would return all the user records for @user’s friends.

Let’s also say your friendships table contains the fields user_id:integer, friend_id:integer, and state:string, where state can either be ‘active’, ‘requested’, or ‘pending’.

You might be tempted to do something like this in your User class:
has_many :friends do
def active
...some query...
end
...and so on for each state
end

I suppose, this would be fine if you only needed one method. You need something for each state, though, and this direction leads to unsightliness quite fast. Enter :extend.

Using this method, you can define methods in a module and call that module using extend. So the association looks like this:
has_many :friends, :through => :friendships, :extend => FriendshipState

FriendshipState is the module where your methods are defined:
module FriendshipState
Order = "users.last_name asc, users.first_name desc"
def pending
find(:all, :conditions => ['friendships.state = ?', 'pending'], : order => Order)
end
def requested
find(:all, :conditions => ['friendships.state = ?','requested'], : order => Order)
end
def active
find(:all, :conditions => ['friendships.state = ?','active'], : order => Order)
end
end

NOTE: there should be no space between ‘:’ and ‘order’. I did that to prevent god forsaken emoticons

The join is handled for you, so @user.friends.active returns all user records for @user’s active friendships; and so on with pending and requested.

SELECT `users`.* FROM `users` INNER JOIN `friendships` ON `users`.id = `friendships`.friend_id WHERE ((`friendships`.user_id = 1) AND (friendships.state = 'active')) ORDER BY users.last_name asc, users.first_name desc

Nice, huh?

A Developers Responsibility

Friday, June 5th, 2009

UCLICK GoComicsWhen I was interviewing at UCLICK one interviewer said to me, In the end, it’s only comics. At the time, that put me at ease. I had just been introduced to programming and was nervous that I would somehow destroy the entire company by writing some errant code. Some years, a GoComics.com rebuild, and a GoComics.com relaunch later I believe that statement is far from true.

We just relaunched GoComics.com with a significant facelift as well as some really tasty goodness under the hood. One thing we recognized in the previous release of GoComics was that people were using the comments more as a meeting place. We decided to encourage that activity by creating more opportunities for people to comment. Another thing we did was add more sharing opportunities. Somewhat to my surprise, GoComic’s Twitter traffic has skyrocketed. Both of these features are evidence that GoComics isn’t only comics, it’s a community. People show up for the comics, but they stay for the camaraderie and are compelled to share what they find across the Internet.

Internally we built a page that lists every comment in real time. It’s fascinating to see what people are talking about. Everything from the mundane – what’s on the dinner menu for the evening – to the profound – a woman’s miscarriage experience and how it affected her feelings on abortion. Of course, there are trolls and troublemakers, but that’s any community. It’s great to watch how people handle them as well. Some get flamed, as would be expected, others are given the benefit of the doubt.

I think in the development process it’s very easy for the developer to lose site of the humanity of the project. You may be developing another entertainment/commerce/management/accounting/whatever application, but the truth is you are creating something for people. Ideally, real people are going to use what you create. For me, there’s a lot of weight in that realization.

The difficulty is in balancing this reality with the ever looming deadline. Whether you are contracting or employed by a company, you’ve, no doubt, got a deadline on your current project. The struggle becomes make it work versus make it work right. There is room for both. I’ve made it my focus to learn best practices, to not be embarrassed to ask, and to learn something new, no matter how minute, everyday. My goal is that this balance will become second nature. Lofty? Perhaps, but certainly attainable.

It is a real honor when something you contributed to becomes important to someone else. I’m proud of the work we’ve done with GoComics. So much of what was done will never be noticed by anyone but those of us that did the work and that’s alright. Active participation may be the highest form of appreciation.

Words escape me…

Thursday, April 17th, 2008

This, apparently, is a honest-to-goodness internal Microsoft video meant to inspire the Vista sales force. If this is real, and you still buy Vista after this, you are mom jeans. [youtube]http://www.youtube.com/watch?v=sPv8PPl7ANU[/youtube]

Funny instant message typo’s

Thursday, April 10th, 2008

If you are like me, and you’re not face it, you spend a lot of time in front of your computer with your Instant Messenger client of choice distracting you on a regular basis. Often in my haste to please an IM buddy I’ll quickly type something and I’ve noticed that I make some funny typo’s. So, I’ve amassed them in a brief list. Feel free to add more in the comments section.

  • fort = for - this is typically the result of a fat finger
  • ti’s = it’s - not an ode to Frank McCourt
  • dong = doing
  • abou tit = about it
  • noi = no i
  • meat = me at
  • pees = peace
  • lame = my wife’s cat

Incidentally, any 10.5 users using Spaces frustrated by iChat’s hostile take over of your machine upon a new chat? Do. Not. Want!

amazon mp3

Tuesday, September 25th, 2007

amznmp3_logo.jpgamazon.com announced their new music download site today, and it is pretty dang good. in fact, it’s good enough to win out over iTunes for me.

‘almost internet famous’ blogger, scott mcnulty, posted a pretty well balanced review of amazonmp3 as well as a contrast/comparison of both services on TUAW. scott’s conclusion puts it perfectly, so skip ahead if you want the nutshell.

either way, iTunes finally has a legit competitor.plus, amazonmp3 carries the last Telecas record (which iTunes don’t… and, i loves me some Telecast).

when taken out of context…

Thursday, September 20th, 2007

from a chat with !jon today.

chat with !jon

scratching the Surface…

Wednesday, May 30th, 2007

surface03-1.jpg

Well played, FakeSteve. Well played, indeed!

Wilkes-Barre(s) PeeCs!

Saturday, February 24th, 2007

wilkesmac.jpgAll I can say is that it is about time higher education started living up to their designated name.  Came across this from a blogger on TUAW.

A university in Wilkes-Barre, PA will remove every PC on campus over the next three years and replace them with the PCs superior counterpart, the Macintosh.  All in all, Wilkes University will replace 1700 computers.  Think Different.

When I was in college there were maybe six Macs on campus.  They were excellent, but neglected by the IT department because frankly they didn’t need any attention.

Kudos to Wilkes University.  I know where my kids will be going!

New glasses… after seven years…

Friday, February 23rd, 2007

new-glasses.jpgI can’t wear contacts. My eyes are too dry or something. Good thing I don’t mind wearing glasses.

I had an eye exam for the first time in years – like seven, actually – and it turns out my eyes have more than doubled in suckiness. So, I took the opportunity to update my frames also.

I made the mistake of first looking for frames I would like on the Country Club Plaza at the Optical Shop of Aspen. I should have remembered the simple equation Plaza + Aspen = Unaffordable, but I didn’t. Thank the Lord for the interweb.

Enter modernglasses.com, while they won’t ever be nominated for a SXSW Web Award, they do offer a great service: boutique level and designer eye frames at great prices. I saved well over $300 on a pair of frames and lenses (with Crizal, no less).

Whilst searching for a new frame my friend !jon asked if I was getting “a pair of glasses like all the ska-kids wear?” Well, I guess so, I told him. “Oh man… those are so lame! They look like your trying to hard to be cool!” Hard to hear your trying to hard to look cool from this guy!

Trying to hard to look cool? Really? Do I come across that way? I always feel like I’m trying to hard to suppress my coolness. Seriously, I have to dial it way down all the time.

I wonder if my friend !jon feels this guy is trying to hard to be cool?

What’s up Thursday…

Thursday, February 1st, 2007

cybermetri.jpgFor those of you that don’t know – and if you don’t it means you don’t watch the Today show – Windows Vista came out on Tuesday.

In an effort to reach those whose souls have been enlightened by the “Get a Mac” ads M$ released a pretty funny viral, soft sell marketing campaign. Demetri Martin, a very funny comic and host of Trendspotting on the Daily Show, was tapped for the main role in this campaign.

I am still a loyal Mac user, but am willing to give credit where credit is due. Check it here (clearification.com). Pretty pretty pretty pr-et-ty clever!

I would also like to learn how to control my glands.