# Using STI in a migration

**URL:** <https://www.ruby-forum.com/t/using-sti-in-a-migration/63003>\
**Category:** Rails\
**Created:** [June 10, 2006, 10:25am UTC](https://www.ruby-forum.com/t/using-sti-in-a-migration/63003 "2006-06-10T10:25:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sshingler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@sshingler](https://www.ruby-forum.com/u/sshingler)\
**Post date:** [June 10, 2006, 10:25am UTC](https://www.ruby-forum.com/t/using-sti-in-a-migration/63003/1 "2006-06-10T10:25:57Z")

</div>

I have some classes in my model which use STI and they work as expected  
in the console. However, when I try to use them in a migration, I get  
“uninitialized constant OfficePhone”, for example. Why doesn’t the  
migration environment pick up the class defs? OfficePhone is defined in  
the model/phone.rb. I put model :phone in the application.rb but my  
migration just doesn’t seem to pick it up. Any ideas?

Steven

---

<div class="post-metadata">

**Author:** ![sshingler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@sshingler](https://www.ruby-forum.com/u/sshingler)\
**Post date:** [June 10, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/using-sti-in-a-migration/63003/2 "2006-06-10T16:51:15Z")

</div>

On 6/10/06, Steven Talcott S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have some classes in my model which use STI and they work as expected  
> in the console. However, when I try to use them in a migration, I get  
> “uninitialized constant OfficePhone”, for example. Why doesn’t the  
> migration environment pick up the class defs? OfficePhone is defined in  
> the model/phone.rb. I put model :phone in the application.rb but my  
> migration just doesn’t seem to pick it up. Any ideas?

You should put each subclass in its own file.  
Phone should be in phone.rb  
OfficePhone should be in office\_phone.rb  
etc.

That will let you get rid of the “model :phone” line, and also let you  
use them without difficulty in migrations.

---

<div class="post-metadata">

**Author:** ![sshingler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@sshingler](https://www.ruby-forum.com/u/sshingler)\
**Post date:** [June 10, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/using-sti-in-a-migration/63003/3 "2006-06-10T18:34:37Z")

</div>

Steven Talcott S. wrote:

> I have some classes in my model which use STI and they work as expected  
> in the console. However, when I try to use them in a migration, I get  
> “uninitialized constant OfficePhone”, for example. Why doesn’t the  
> migration environment pick up the class defs? OfficePhone is defined in  
> the model/phone.rb. I put model :phone in the application.rb but my  
> migration just doesn’t seem to pick it up. Any ideas?
> 
> Steven

Along with what has already been mentioned:

“Don’t use your ActiveRecord models in your migrations or your life will  
be miserable”

[http://rails.techno-weenie.net/tip/2006/2/23/safely\_using\_models\_in\_migrations](http://rails.techno-weenie.net/tip/2006/2/23/safely_using_models_in_migrations)

---

<div class="post-metadata">

**Author:** ![sshingler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@sshingler](https://www.ruby-forum.com/u/sshingler)\
**Post date:** [June 10, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/using-sti-in-a-migration/63003/4 "2006-06-10T16:57:29Z")

</div>

Very true. What you’ll find is that you can use OfficePhone as long as  
you have already used Phone. Using Phone will cause phone.rb to be  
loaded, which inadvertently also loads the definition of OfficePhone.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![sshingler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@sshingler](https://www.ruby-forum.com/u/sshingler)\
**Post date:** [June 10, 2006, 9:29pm UTC](https://www.ruby-forum.com/t/using-sti-in-a-migration/63003/5 "2006-06-10T21:29:47Z")

</div>

Richard L. wrote:

> Along with what has already been mentioned:
> 
> “Don’t use your ActiveRecord models in your migrations or your life will  
> be miserable”
> 
> [http://rails.techno-weenie.net/tip/2006/2/23/safely\_using\_models\_in\_migrations](http://rails.techno-weenie.net/tip/2006/2/23/safely_using_models_in_migrations)

Thanks, I appreciate this and I too discovered that if I use the parent  
class, I can get away with it.

I am writing a one-time series of migrations of a production database to  
a new railsified schema so my model is evolving together with the  
migrations and it helps me a lot to go back and re-run the series of  
migrations constantly as I define the models. I certainly understand  
the long term problem of keeping migrations around that use obselete  
models but once I am in production, I will never need these migrations  
again and will probably restart the migration counter at 0.

Thanks again for the tips! After cutover, I certainly will include any  
required model defs in my migrations.

Steven
