# Model userstamp

**URL:** <https://www.ruby-forum.com/t/model-userstamp/66977>\
**Category:** Rails\
**Created:** [August 30, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/model-userstamp/66977 "2006-08-30T18:42:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 30, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/model-userstamp/66977/1 "2006-08-30T18:42:16Z")

</div>

hey, i have something like this for the user timestamp (created\_by and  
updated\_by), i have seen this on [http://delynnberry.com/pages/userstamp/](http://delynnberry.com/pages/userstamp/)  
and [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample)

#adding ‘created\_by’ and ‘updated\_by’ to all models  
module ObjectStatistics  
def self.append\_features(base)  
base.before\_create do |model|  
model.created\_by ||= User.backend\_user.id if  
model.respond\_to?(:created\_by)  
end  
base.before\_save do |model|  
model.updated\_by = User.backend\_user.id if  
model.respond\_to?(:updated\_by)  
end  
end

# belongs\_to :created\_by, :class\_name =\> “User”, :foreign\_key =\>

“created\_by”

# belongs\_to :updated\_by, :class\_name =\> “User”, :foreign\_key =\>

“updated\_by”

end

my usermodel  
class User \< ActiveRecord::Base  
include ObjectStatistics  
cattr\_accessor :backend\_user

```
def to_s
	return self.login
end

```

end

my productmodel  
class Product \< ActiveRecord::Base  
include ObjectStatistics  
end

in my application controller i have this:

before\_filter :set\_backend\_user  
def set\_backend\_user  
User.backend\_user = User.find(session[:backend].id) unless  
session[:backend].nil?  
end

Now this al works! 🙂

But when i do item.created\_by =\> i get “1”  
this is the id from the module (User.backend\_user.id ), but i want the  
login.

Now my question is, how can i implement this below in the module so i  
dont need to type it in every model?

belongs\_to :created\_by, :class\_name =\> “User”, :foreign\_key =\>  
“created\_by”  
belongs\_to :updated\_by, :class\_name =\> “User”, :foreign\_key =\>  
“updated\_by”

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 30, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/model-userstamp/66977/2 "2006-08-30T19:31:46Z")

</div>

I had the same question about how to implement the belongs\_to in a more  
dry fashion. I din’;t figure it out, os i have that declaration in  
most all of my models. Not ideal.

As for displaying the login, you could override the accessor

def self.created\_by  
self.created\_by.login  
end

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 31, 2006, 12:46am UTC](https://www.ruby-forum.com/t/model-userstamp/66977/3 "2006-08-31T00:46:29Z")

</div>

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”)  
end  
…  
end

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.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 31, 2006, 1:10am UTC](https://www.ruby-forum.com/t/model-userstamp/66977/4 "2006-08-31T01:10:33Z")

</div>

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 = MyController.new  
klass = class \<\< @controller; self; end  
klass.send(:undef\_method, :something\_expensive)  
klass.send(:define\_method, :something\_expensive, lambda {nil})  
end

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  
end

module ModuleWithExpensiveMethod  
def something\_expensive  
sleep(100)  
end  
end

### 

## thanks

Yan Pritzker

> **[skwpspace](https://skwp.wordpress.com/)**
>
> musings on software, food, and life, by Yan Pritzker

[http://planyp.us](http://planyp.us)

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 31, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/model-userstamp/66977/5 "2006-08-31T13:35:35Z")

</div>

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  
acts\_as\_monitored  
end

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).

[http://svn.viney.net.nz/things/rails/plugins/acts\_as\_monitored](http://svn.viney.net.nz/things/rails/plugins/acts_as_monitored)

-Jonathan.

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 31, 2006, 10:36am UTC](https://www.ruby-forum.com/t/model-userstamp/66977/6 "2006-08-31T10:36:15Z")

</div>

thx it works 😉

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 1, 2006, 12:17am UTC](https://www.ruby-forum.com/t/model-userstamp/66977/7 "2006-09-01T00:17:21Z")

</div>

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 = MyController.new  
class \<\< @controller  
undef something\_expensive # not strictly necessary  
def something\_expensive; end  
end  
end

## But if you are getting into mocking and stubbing, you might like to have a look at Mocha at [http://mocha.rubyforge.org](http://mocha.rubyforge.org) which can make your life a bit easier.

James.

> **[James Mead](https://jamesmead.org/)**
>
> Ruby Developer, author of Mocha, and ex-Antarctic Hero.
