Thursday, November 29, 2007

ActiveRecord Columns

It can be hard to keep up on side projects, there are just so many other things around the house demanding attention. This week, I've been spending my time demolishing a brick BBQ/planter in the back yard. I've removed 3 cubic meters of dirt and brick! Last night I finished that, so I started to think about the side project I'm working on. As I did, I thought back to the WTF moment I wrote about a while ago, and I was afraid that I was wrong. Totally wrong. Idiotically wrong in a way that would demonstrate that I am a complete total and utter Ruby on Rails noob.

It's interesting about blog posts. I've added Feedburner so I have a bit better an idea of how many people are reading this. Before, I thought it was just a couple of friends and then only when I bugged them, "Hey, did you see my new post?". Now, it seems I have a readership (albeit minimal).

That means that any mistakes I make here are really, really public and enduring. Of course, for me that's part of the point of the blog. I want to be able to go back and see how my ideas change over the years.

Anyways, back to Rails. I was afraid that this code was wrong:

def update_my_status(user, status) if (a_party == user) self.a_party_status = status else self.b_party_status = status end end

I was sitting there thinking, "Wait a second, Ruby says that data members have an '@' in front of them!", quickly followed by, "Nuts, so, do I edit or delete the previous post?"

Just to be sure, I went and fired up the rails console. Very, handy, much recommended. I modified the code to look like this:

class Call < ActiveRecord::Base def my_status(user) if (@a_party == user) @a_party_status else @b_party_status end end ...

I then called it in the console:

>> call = Call.find(:first) => # >> call.my_status(2) => nil

Whew, off the hook. It seems that Ruby has "method missing" functionality, but not "member missing" functionality. So, when I put an '@' in front of it, ActiveRecord doesn't know to look up the value in @attributes. Interesting, and a bit more learned about the internals of ActiveRecord and Rails.


Anonymous said...

Whereas the dog leaf country snows the blower, Ned can't quite understand whereto Lo! Over yonder by the gale blue streamlined steamliner. What will we do while the waiter watches? Most tame white wales make huge showers. We will play a criquet game or a match.

Jason Pollock said...

That's some pretty awesome originality anonymous.