Sunday, September 5, 2010

Skipping Active Record Callbacks in Rails

It is often desirable to skip ActiveRecord callbacks defined on rails model objects. For a somewhat contrived(but true life) example, let's say we have an ActiveRecord::Observer that defines an after_save callback which can be called on the observed models. Sort of like "pseudo polymorphic callback inheritance"?


Updating, saving, validating, or performing other ActiveRecord operations on our model object inside the callback itself can cause stack overflows and other undesired behavior to occur, and it's not always obvious where the problem is occuring.


One elegant solution to avoiding this particular problem can be achieved by extending ActiveRecord::Observer with a skip_callback method. With the following code, we get a singleton method on our observer model. This method accepts a block of (almost) any arbitrary code. The method first stubs out our callback as a boolean, yields to our code, and then re-defines the callback method as originally defined:


config/initializers/active_record_observer_extensions.rb:


class ActiveRecord::Observer
  def self.skip_callback(callback, &block)
    method = instance_method(callback)
    remove_method(callback) if respond_to?(callback)
    define_method(callback){ true }
    yield
    remove_method(callback)
    define_method(callback, method)
  end
end

Where I am assuming your observer looks something like this:

app/models/fyle_observer.rb:

class FyleObserver < ActiveRecord::Observer
  observe :script, :document, :image

  def after_save(observed)
      ...assign upload attributes, etc...
      FyleObserver.skip_callback(:after_save) do
        Fyle.update_attributes(:mime_type => 'application/msword')
    end
  end

end

Code Explanation: If you tried to update Fyle inside the after_save method in FyleObserver without skipping the callback, your application would surely freeze up because you would trigger an endless chain of after_save callbacks(like standing between two mirrors and seeing infinity!) Here's what happens in the skip_callback method: First the callback is placed into the temp variable named method. Then the original callback named as a parameter is removed from the ActiveRecord::Observer class if it responds to it. Next it is defined as a simple method that returns true. Then the code comprising our block is evaluated before finally the simple method is removed and the original re-defined.

Tuesday, August 24, 2010

Overriding ActiveRecord methods

I recently needed to delete a file along with an active record model, and I wasn't sure about overriding rails' ActiveRecord::Base#delete method, but the following code worked without a hitch in my model:

def delete
 File.delete( File.join( Rails.root, fyle.path ) )
 super
end

However, overriding the initialize method in the same way is not recommended unless you really know what you are doing.

Sunday, January 24, 2010

Grade Point Median

Before dropping out of graduate school, I did my B.S. in Statistics with a minor in business administration. My real focus was computer science, however, and I was learning programming when I could have been doing better homework. I graduated with a 3.1 gpa, but I argue that a GPM(grade point median) is a better way to calculate an aggregate of academic performance. In my case, two F's during one bad quarter in 2005 during a breakup with my fiance are high influence points unrelated to academic ability. I think even a 5% trimmed mean would put me above a 3.3 gpa.

The point is, although some academics are pretty good at analyzing their student population, along with individual track records, and grading accordingly, few students are going to print a stable performance history. Life is too volatile. For the few who are regularly outstanding, they won't lose anything by cutting a few of their worst and best grades. But for the unfortunate students experiencing the turbulence of life, a GPM or trimmed mean provides a more accurate representation of typical performance.

Saturday, January 23, 2010

Data mining gets a bad rap

Yes, the business landscape is very competitive. Every day we hear about new business intelligence solutions and advancements in computer technology that make us wonder just what the heck is going on in some of the nations largest corporations with the data they gather from us. When I hear another appraiser complain about how data mining is destroying their livelihood, I feel sympathetic to their struggle in this horrible market, but I also feel the urge to shed some light on the virtues of data mining.

Data mining and artificial intelligence in general may give someone the resources to be evil, but the same thing has been said about computers(remember apple's 1984 commercial about big brother?). I don't see big brother cracking down on the masses, in fact, isn't google cracking down on china for censorship? Many people have gotten a call from their credit card company about fraudulent activity on their accounts. Guess how the credit card company found out about the fraud? Yep. More than likely, Data mining. Guess how some important advancements in technology, including medical advancements, are discovered? Data mining.

So next time you hear someone extolling the evils of data mining and how its ruining their livelihood, ask them why they haven't yet built a web crawler to put google out of business, or designed a low overhead online financial institution. Maybe they just need a nudge in the right direction.

Friday, November 13, 2009

New Models of the Organization

I have been doing a lot of thinking lately about how many existing models of the organization are insufficient in addressing some important sociological issues. Most important of these, I believe, are the general desire for autonomy, individuality, and self-expression. I feel that most people appreciate being actively involved in the organizational process. Most experts with deep knowledge of human behavior agree that human beings put enormous value on independence and self-expression. So then why is it that so many organizations are engineered in such a way as to hinder these very basic societal needs instead of fostering them? Much new research points to the latter as more effective in the achievement of a vast array of organizational objectives, most important of which may be improvements in marketing and market prediction, research and development, administrative and operational processes, communications, and human capital risk management.

In this new technological age of data mining, crowdsourcing, and social network analysis, many companies still haven't realized the enormous gains to be achieved through systematic de-centralization and the enabling of self-organizing teams. It is well accepted in operations research, for example, that the worst team dynamics result from appointed teams and over-direction from outsiders. It is also a fact that the most successfully innovative research and development teams in history were given virtually non-existent timelines. As someone who has worked for many years under multiple strict daily production deadlines appraising real estate, I see major implications here in terms of quality control. It goes without saying that the highest form of motivation is that which is generated internally, and many 'incentive programs' are way off the mark.

While the most successful organizations today have wholeheartedly embraced the new paradigm, there are many destined to suffer through the process of Creative Destruction. Fortunately, models are made to be destroyed so we can learn from them, and the stakes are higher now than ever.