# Models, Modules, and Callbacks

**URL:** <https://www.ruby-forum.com/t/models-modules-and-callbacks/52693>\
**Category:** Rails\
**Created:** [January 18, 2006, 1:03am UTC](https://www.ruby-forum.com/t/models-modules-and-callbacks/52693 "2006-01-18T01:03:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [January 18, 2006, 1:03am UTC](https://www.ruby-forum.com/t/models-modules-and-callbacks/52693/1 "2006-01-18T01:03:27Z")

</div>

There’s been some discussion about this before but I think I have a  
unique problem.

I am trying to use modules with models to avoid naming conflicts while  
connecting to more than one database.

Everything seems to be working ok in that we have:

- Model definitions in models/namespace/model.rb
- Controllers accessing models by Namespace::Model
- Controllers loading models using « model ‘namespace/model’ »

The only problem I am having is callbacks like before\_create,  
after\_create, etc seem to get called multiple times. For example…

module A  
class B \< ActiveRecord::Base  
before\_create :log\_create

```
def log_create
  puts '>>> Creating...'
end

```

end  
end

And then do A::B.create, I get multiple “\>\>\> Creating…” messages,  
but only one object in my table.

I am guessing because, in development, require\_dependency loads the  
file multiple times and the module mess things up somehow - my  
callbacks keep piling on top of one another.

Thoughts?

JD

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [January 18, 2006, 1:09am UTC](https://www.ruby-forum.com/t/models-modules-and-callbacks/52693/2 "2006-01-18T01:09:29Z")

</div>

> Thoughts?

The only thing that pops into my mind is a question. What type of  
relationships does A::B have with other models (habtm, has\_many,  
etc…)?

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [January 18, 2006, 3:19am UTC](https://www.ruby-forum.com/t/models-modules-and-callbacks/52693/3 "2006-01-18T03:19:13Z")

</div>

I ran into similar problem when trying to use Modules with my models  
and got no response from anyone so I’m not sure where the issue lies.  
I had very strange behaviour when I tried doing this, with the class  
basically getting loaded once, then forgotten about or lost somewhere  
in the mist…its very odd.

Know this doesn’t help, but your not the only one having issues with  
this at the moment. Anyone else have any thoughts on this? I find it  
hard to believe everyone keeps the models on the top level namespace.

-Nick

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [January 18, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/models-modules-and-callbacks/52693/4 "2006-01-18T16:02:17Z")

</div>

> The only thing that pops into my mind is a question. What type of  
> relationships does A::B have with other models (habtm, has\_many, etc…)?

In my case, it has a belongs\_to and a has\_many, and it does use both  
aggregation heavily. How might this complicate things? The callbacks  
did not get called multiple times before we added the modules.
