# (Dreaded) STI, belongs\_to

**URL:** <https://www.ruby-forum.com/t/dreaded-sti-belongs-to/193077>\
**Category:** Rails\
**Created:** [August 10, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/dreaded-sti-belongs-to/193077 "2010-08-10T20:29:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [August 10, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/dreaded-sti-belongs-to/193077/1 "2010-08-10T20:29:40Z")

</div>

Having problems with STI. Names below have been changed to protect the  
innocent. Imagine a system that allows someone to apply for one of two  
different types of school scholarships. Each scholarship goes through a  
different review process, represented by a state machine (this is not a  
state machine question). So there are two state machine classes that  
differ slightly and subclass a generic StateMachine:

class ScholarshipApplication  
belongs\_to :state\_machine  
end

class StateMachine  
end

class StateA \< StateMachine  
has\_one :application  
end

class StateB \< StateMachine  
has\_one :application  
end

The problem is I can get a ScholarshipApplication instance to tell me  
its StateMachine, but the StateMachine can’t tell me it’s  
ScholarshipApplication. E.g.:

app = ScholarshipApplication.create  
state = StateA.create  
app.state\_machine = state  
app.save  
app.state\_machine # returns what it should  
sm = app.state\_machine  
sm.scholarship\_application # throws error: “Unknown column  
scholarship\_applications.state\_a\_id…”

So obviously I understand why this fails, since I don’t have a  
state\_a\_id column in scholarship\_applications. I just don’t understand  
whether what I’m doing is achievable and if so, what magic configuration  
makes it all work. E.g. should my has\_one statements be in the  
superclass? Should I reverse the has\_one and belongs\_to? Something  
else?

Thanks!

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [August 10, 2010, 8:39pm UTC](https://www.ruby-forum.com/t/dreaded-sti-belongs-to/193077/2 "2010-08-10T20:39:44Z")

</div>

Gaudi Mi wrote:

> Having problems with STI. Names below have been changed to protect the  
> innocent. Imagine a system that allows someone to apply for one of two  
> different types of school scholarships. Each scholarship goes through a  
> different review process, represented by a state machine (this is not a  
> state machine question). So there are two state machine classes that  
> differ slightly and subclass a generic StateMachine:
> 
> class ScholarshipApplication  
> belongs\_to :state\_machine  
> end
> 
> class StateMachine  
> end
> 
> class StateA \< StateMachine  
> has\_one :application  
> end
> 
> class StateB \< StateMachine  
> has\_one :application  
> end
> 
> The problem is I can get a ScholarshipApplication instance to tell me  
> its StateMachine, but the StateMachine can’t tell me it’s  
> ScholarshipApplication.  
> […]

Perhaps because you should have your has\_one read  
has\_one :scholarship\_application !

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [August 10, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/dreaded-sti-belongs-to/193077/3 "2010-08-10T21:11:44Z")

</div>

> Perhaps because you should have your has\_one read  
> has\_one :scholarship\_application !

Sorry I should have stated in the original post that I was typing  
pseudocode here; the actual code doesn’t have this mistake.

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [August 10, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/dreaded-sti-belongs-to/193077/4 "2010-08-10T21:46:07Z")

</div>

Gaudi Mi wrote:

> > Perhaps because you should have your has\_one read  
> > has\_one :scholarship\_application !
> 
> Sorry I should have stated in the original post that I was typing  
> pseudocode here; the actual code doesn’t have this mistake.

Then you _might_ need a polymorphic association. I’m not sure: I tend  
not to use STI.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [August 11, 2010, 6:25pm UTC](https://www.ruby-forum.com/t/dreaded-sti-belongs-to/193077/5 "2010-08-11T18:25:39Z")

</div>

On Aug 10, 2:29 pm, Gaudi Mi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> sm.scholarship\_application # throws error: “Unknown column  
> scholarship\_applications.state\_a\_id…”

Passing the :foreign\_key option to the has\_ones will definitely work.  
You might also try hoisting the has\_one into StateMachine (if that  
makes sense for your models), as it’s guessing the foreign key named  
based on the class where it’s declared (_probably_ - I’ve not poked  
the source to check this).

–Matt J.
