# Bypassing automatic STI generation

**URL:** <https://www.ruby-forum.com/t/bypassing-automatic-sti-generation/184988>\
**Category:** Rails\
**Created:** [March 11, 2010, 3:26pm UTC](https://www.ruby-forum.com/t/bypassing-automatic-sti-generation/184988 "2010-03-11T15:26:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 11, 2010, 3:26pm UTC](https://www.ruby-forum.com/t/bypassing-automatic-sti-generation/184988/1 "2010-03-11T15:26:39Z")

</div>

Hi,

Let’s say I have the following:

Product \< AR  
Memberships \< Product

I know that the product with id 10 has its type column set to  
“Membership”, but still I’d like to treat it as a regular product.

So if I do: Product.find(10), Rails returns a Membership object.

Is it possible to tell rails to not care about STI and to simply return  
a Product object?

Regards,

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 11, 2010, 3:33pm UTC](https://www.ruby-forum.com/t/bypassing-automatic-sti-generation/184988/2 "2010-03-11T15:33:25Z")

</div>

On 11 March 2010 14:26, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it possible to tell rails to not care about STI and to simply return  
> a Product object?

But it _is_ a Membership object… ?:-/

But if you _have_ to have it as a Product for some reason, you can use  
the ActiveRecord::Base “becomes” method:

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002334)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 11, 2010, 3:39pm UTC](https://www.ruby-forum.com/t/bypassing-automatic-sti-generation/184988/3 "2010-03-11T15:39:59Z")

</div>

> But it _is_ a Membership object… ?:-/
> 
> But if you _have_ to have it as a Product for some reason, you can use  
> the ActiveRecord::Base “becomes” method:  
> [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002334)  
> It’s a Membership indeed, but in the admin section of my app, I don’t  
> need to recode a specific controller for memberships, they can use the  
> products\_controller which is fine and defining :controller =\> “products”  
> won’t do the trick as some actions cannot be shared.

#becomes is exactly what I was looking for, I didn’t know about this  
method and it’s not featured in AWDWR 3rd Ed.

Thanks!

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 11, 2010, 3:50pm UTC](https://www.ruby-forum.com/t/bypassing-automatic-sti-generation/184988/4 "2010-03-11T15:50:23Z")

</div>

On 11 March 2010 14:39, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > But it _is_ a Membership object… ?:-/
> 
> It’s a Membership indeed, but in the admin section of my app, I don’t  
> need to recode a specific controller for memberships, they can use the  
> products\_controller which is fine and defining :controller =\> “products”  
> won’t do the trick as some actions cannot be shared.

Makes sense.

> #becomes is exactly what I was looking for, I didn’t know about this  
> method and it’s not featured in AWDWR 3rd Ed.

Kewl. Glad to help.  
AWDWR is a good book - but it’s not all-encompassing… You need to  
get familiar with navigating the Ruby and Rails API documentation to  
find the rest of the cool stuff 🙂
