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

Heading to Portland for RailsConf... by foot

Posted by Wed, 16 May 2007 22:21:00 GMT

Yay! It’s almost conference time… and I’m almost completely thrilled!

Why am I not more thrilled? Well, mainly because RailsConf is being hosted here in Portland, which means that I don’t get to travel by train like we did last year via The Argon Express. (ah… the memories)

One perk of being here already… is that I get to act as a tour guide to visitors. For example, earlier today… Josh Susser (hasmanyjosh) joined Graeme and I for lunch in downtown Portland, OR. It was exciting to hear about how he and his fellow Rubyists at Powerset are using Ruby and Rails for various projects. John also spent a few minutes introducing us to Merb, which some people think will become popular in near future. We’re expecting more visitors to come by the offices over the next few days.

Josh Susser and Graeme Nelson

If you’re in Portland already (Wednesday)... you might head over to the Lucky Lab for a game of Werewolf, which I believe Michael Buffington is organizing the event. I’m going to try to make it… maybe I’ll see you there!

Flickr Group

I created a flickr group named RailsConf 2007, and started to use the tag railsconf2007 for flickr photos. I look forward to seeing all your photos from the event!

IRC Channel

As of this afternoon, there are almost 20 people hanging out in #railsconf on freenode. Stop by and introduce yourself!

Portland Revealed

If you haven’t already subscribed to our feed, you might have missed that the PLANET ARGON team has been posting several articles about things to do, see, and drink in Portland during your visit to RailsConf. Here are a few that we’ve posted so far.

You can also peak around GoSeeOregon (a Rails application!) to find places around town to go see.

Q&A: ActiveRecord Observers and You

Posted by Sat, 28 Apr 2007 22:28:00 GMT

Yesterday, I wrote a short post titled, Observers Big and Small, about using Observers in your Rails applications.

The following questions were raised in the comments.

When should I use an Observer?

Eric Allam asks…

“Why not just use ActiveRecord callback hooks instead of Observers? Are Observers more powerful or is it just a matter of preference?”

Eric, this is an excellent question. I’d say that a majority of the time, using the ActiveRecord callbacks in your models is going to work for your situation. However, there are times that you want the same methods to be called through callbacks. For example, let’s take a recent problem that we used an observer to solve.

Graeme is working on implementing Ferret into a project that we’re developing for a client. With the use of Ferret, we can index and later search through content over several objects into a format that makes sense for our implementation goals. Each time an object is created and updated, we have to update our Ferret indexes to reflect these changes. The most obvious location that we can call our indexing methods is in each models’ callbacks, but this violates the DRY[1] principle. So, we created an Observer, which observes each of the models that need these methods to be called. In fact, as far as we’re concerned, the fact that we’re indexing some of its data, is none of its business. We only want our models to be concerned with that they’re designed to be concerned about. We may opt to change our indexing solution in the future and we’d just need to rethink that at the Observer level and not change anything about the business logic in our models.

This is the sort of scenario when using an Observer makes great sense in your application.

Logging from an Observer

Adam R. asks…

“I’d also like the ability to use the logger from within an observer, but that’s another issue.”

I assume that you are referring to the logger method? I always forget to even use that method. I do know that the following works just fine in an Observer.


class IndexObserver < ActiveRecord::Observer
  observer Article, Editorial, BlogPost, ClassifiedAd

  def after_save(model)
    RAILS_DEFAULT_LOGGER.warn("Every single day. Every word you say. Every game you play. Every night you stay. I'll be watching you.")
    # execute something fun
  end
end  

This will output to your log file without any problem.

This reminded me of when I used to want to log from Unit Tests.

(few minutes later)

Okay, I just attempted to use logger from an Observer and you’re right… it doesn’t currently work. There is a simple fix though, just extend ActiveRecord::Observer to add a logger method like so and require it in config/environment.rb (much like I did in with unit tests).


# lib/observer_extensions.rb
class ActiveRecord::Observer
  def logger
    RAILS_DEFAULT_LOGGER
  end
end

This will give you a solution to that problem.


class FooObserver < ActiveRecord::Observer
  observer Foo

  def after_save(model)
    logger.warn("I wonder if the #{address.class} knows that I've been watching it all along?")
  end
end  

Observers Spy for Us

Most often, I look at Observers as being the guys that I hire to spy on my models. I don’t want my models to know that they’re being spied on and I’d like to keep it that way. They don’t solve all of our problems and it’s easy to overuse them. However, I have found several cases that they made a lot of sense and most of those cases have been where we’ve had the same things occurring in our model’s callbacks.

If you have other questions related to Observers, feel free to let me know. If you’re already using Observers, perhaps you could post a comment and/or blog post response with an example of when and how you use Observers in your Rails applications.

Related Posts

1 Don’t Repeat Yourself

Do Your Views in Ruby on Rails need a cleaning service?

Posted by Wed, 28 Mar 2007 02:55:00 GMT

I’ve been working on a project with Graeme, and we’re spending some time cleaning up some RHTML views.

He posted an article earlier, titled, Dirty Views? Clean them up!, where he asks the following.

“I am also looking for more information on best practices with views in Rails. There doesn’t seem to be much information on the subject.”

We’re starting to re-evaluate how we approach our views and are curious what other teams are doing… especially if you have a team thats approx 1/2 designers… 1/2 developers per project. We’ll be reviewing some of the other options for the View layer over the coming week(s) and welcome any suggestions/insight to this area of Rails… head over to Graeme’s blog and share your thoughts. :-)

Meet... Chris, Graeme., and Gary

Posted by Mon, 26 Feb 2007 17:01:00 GMT

Okay, this is a little overdue… but better late than never! ;-)

We’ve had several new people start with PLANET ARGON over the past few months. Some of them are blogging about their experience of working with Ruby on Rails and being a part of our team. I wanted to quickly introduce you to a few of them and their blogs, which I hope that you consider subscribing to.

Chris

For quite some time, we’ve been needing more design assistance, so late last year… we hired Chris Griffin, who moved here last year from Florida. He’s our new User Interface Designer and gets to work within the Rails environment everyday with the rest of us. It seems that Brian and Chris worked over the weekend to get his new blog up. Chris is self-proclaimed genius. I suggest that you keep an eye on his blog… because I’m sure it’s going to be a pretty active one. Chris joining our team marks a pivotal point in our teams evolution as we continue to place more emphasis in our Design and Development process on the User Experience.

Graeme

Graeme Nelson

Our newest hire is Graeme Nelson, who recently moved to Portland from Seattle. He just joined our Design and Development team and if you’ve been reading the Rails-related blogs, you might have seen his blog already. He’s been blogging a lot about using RSpec with Rails and other fun things. He’s been contracting with us since the start of the year and I’m really excited that he’s accepted a job offer and joined the team!

Gary

Gary eats sushi

Last… but not least is Gary Blessington. I believe that I first offered Gary a job with PLANET ARGON about 2 1/2 years ago when we were still focused on PHP/PostgreSQL…. but PHP apparently wasn’t enough of a catalyst. Gary and I previously worked together at Imark Communications several years ago, when I first started doing web development. He was the senior developer on the team and was an important mentor during my early days of developing in a professional environment. Late last year, he hung up his .NET tool belt to become our Design and Development Director. He started blogging earlier this year and is sharing his experience of switching from .NET to Ruby on Rails.

I’ll introduce the others as they start blogging and such. :-)