Model userstamp

module ObjectStatistics
  def included(other)
    other.send(:belongs_to, :created_by, :class_name => "User",
:foreign_key =>"created_by")
    other.send(:belongs_to, :updated_by, :class_name => "User",
:foreign_key =>"updated_by")

This will call the belongs_to method on the class that the module is
included in. You may need to fiddle with the syntax, can't test this
at the moment.


Hi all,

I am trying to do a functional test on a controller that mixes in a module with an expensive method, but I would like to mock out that method for my testing purposes. I can't seem to do it. Here's what I have tried

def setup
  @controller =
  klass = class << @controller; self; end
  klass.send(:undef_method, :something_expensive)
  klass.send(:define_method, :something_expensive, lambda {nil})

I have also tried to do the less idiomatic but more straightforward
def @controller.something_expensive; nil; end

but that didn't help, either. The undef_method fails because it can't find the method (I guess becaues it's actually defined in the module). The other way just silently fails to accomplish its objective. I've googled some but am somewhat perplexed. Ideas?

### this is what the controller looks like ###

class MyController
  include ModuleWithExpensiveMethod

module ModuleWithExpensiveMethod
  def something_expensive

I’ve got a plugin which handles this very simply. You just add two columns to the table, created_by_id and updated_by_id, then declare:

class Person < ActiveRecord::Base

And the fields will be updated accordingly. This is a slightly nicer solution as it allows you to include the associations with a find.

The plugin assumes the current user can be accessed through User.current (you can change this if you need).


Both of these techniques work fine when I try your examples - are you sure your example illustrates the problem you are seeing?

A third way is like this…

def setup

@controller =
class << @controller
undef something_expensive # not strictly necessary
def something_expensive; end

But if you are getting into mocking and stubbing, you might like to have a look at Mocha at
which can make your life a bit easier.