# Problems with STI in has\_many/belongs\_to in Rails 1.1

**URL:** <https://www.ruby-forum.com/t/problems-with-sti-in-has-many-belongs-to-in-rails-1-1/56708>\
**Category:** Rails\
**Created:** [April 3, 2006, 7:09am UTC](https://www.ruby-forum.com/t/problems-with-sti-in-has-many-belongs-to-in-rails-1-1/56708 "2006-04-03T07:09:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8dc957/32.png) [@lori](https://www.ruby-forum.com/u/lori)\
**Post date:** [April 3, 2006, 7:09am UTC](https://www.ruby-forum.com/t/problems-with-sti-in-has-many-belongs-to-in-rails-1-1/56708/1 "2006-04-03T07:09:29Z")

</div>

I have a problem that surfaced in my attempt to upgrade my application  
to Rails 1.1.

We have a STI model on the “belongs\_to” side of a has\_many/belongs\_to  
relationship. All my unit tests for this model pass, and the  
relationships all seem to work fine.

But in my functional tests, I’m getting errors. I’ve traced it back  
into the call to the has\_many association returning nil (To put it in  
familiar terms, this is like author.posts returning nil). I was under  
the impression that a call to something like author.posts would NEVER  
return nil, you would just get an empty array. In either case, it’s  
bad, because in my functional test, it should be getting at least one  
“post” back.

Does anyone have any insight here? I’ve been banging my head against a  
brick wall for a couple of days now, and I’m about ready to give up.

–  
Lori O.  
[http://blog.dragonsharp.com](http://blog.dragonsharp.com)

---

<div class="post-metadata">

**Author:** ![lori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8dc957/32.png) [@lori](https://www.ruby-forum.com/u/lori)\
**Post date:** [April 4, 2006, 12:05am UTC](https://www.ruby-forum.com/t/problems-with-sti-in-has-many-belongs-to-in-rails-1-1/56708/2 "2006-04-04T00:05:03Z")

</div>

On Monday, April 03, 2006, at 5:06 AM, Lori O. wrote:

> the impression that a call to something like author.posts would NEVER  
> return nil, you would just get an empty array. In either case, it’s  
> bad, because in my functional test, it should be getting at least one  
> “post” back.

Ok, I’ve narrowed this down to a regression in eager loading behavior.

Let me reframe the example a little. Let’s say we have Author and Topic  
and Post. Now, author has\_many :topics, and topic has\_many :posts. To  
throw a wrench in the works, the Author also has a “current\_topic” (So  
they can logoff, and login, and start at the same place they left off),  
represented by current\_topic\_id in the authors table.

In Rails 1.1, when I go after author.current\_topic.posts, I get nil. In  
Rails 1.0, this works fine.

class Author  
has\_many :topics  
belongs\_to :current\_topic, :class\_name =\> “Topic”, :foreign\_key =\>  
“current\_topic\_id”  
end

class Topic  
has\_many :posts  
has\_one :current\_for\_author, :class =\> “Author”, :foreign\_key =\>  
“current\_topic\_id”  
end

class Post  
belongs\_to :topic  
end

I’ve tried using :include =\> :posts on the belongs\_to :current\_topic,  
but that really doesn’t work (the generated SQL is bizarre). Any other  
ideas are welcome.

Now that I understand it, I can work around it, but I think it qualifies  
as a bug…

–  
Lori O.  
[http://blog.dragonsharp.com](http://blog.dragonsharp.com)

---

<div class="post-metadata">

**Author:** ![lori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8dc957/32.png) [@lori](https://www.ruby-forum.com/u/lori)\
**Post date:** [April 4, 2006, 12:54am UTC](https://www.ruby-forum.com/t/problems-with-sti-in-has-many-belongs-to-in-rails-1-1/56708/3 "2006-04-04T00:54:04Z")

</div>

On Monday, April 03, 2006, at 10:04 PM, Lori O. wrote:

> Rails 1.0, this works fine.  
> “current\_topic\_id”  
> Now that I understand it, I can work around it, but I think it qualifies  
> as a bug…

I guess I should also point out that in my case “Post” is an STI class.

–  
Lori O.  
[http://blog.dragonsharp.com](http://blog.dragonsharp.com)
