# Still Having Problems With :through When Going To Same Table

**URL:** <https://www.ruby-forum.com/t/still-having-problems-with-through-when-going-to-same-table/74722>\
**Category:** Rails\
**Created:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/still-having-problems-with-through-when-going-to-same-table/74722 "2006-11-07T10:53:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/still-having-problems-with-through-when-going-to-same-table/74722/1 "2006-11-07T10:53:47Z")

</div>

I am having a problem with doing a :through that goes back to the same  
table.

The following are my two classes:

> > > > >

class User \< ActiveRecord::Base  
has\_many :spanks  
has\_many :spanked, :through =\> :spanks, :source =\> :spanked\_user  
has\_many :was\_spanked\_by, :through =\> :spanks, :source =\> :user  
end

class Spank \< ActiveRecord::Base  
belongs\_to :spanker, :class\_name =\> “User”, :foreign\_key =\> “user\_id”  
belongs\_to :spankee, :class\_name =\> “User”, :foreign\_key =\>  
“spanked\_user\_id”  
end

db\_table:spanks  
id | user\_id | spanked\_user\_id | created\_at | updated\_at

> > > > >

I want to be able to make statemets like:

john = User.find\_by\_nickname(“john”)  
john.spanked  
john.was\_spanked\_by

Can I do this without using a :finder\_sql?

The above does not work… but the following does:

john.spanks  
spank = Spank.find(1)  
spank.spankee  
spank.spanker

Help… help… I get the following error:

ActiveRecord::HasManyThroughSourceAssociationNotFoundError:ActiveRecord::HasManyThroughSourceAssociationNotFoundError

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 7, 2006, 10:55am UTC](https://www.ruby-forum.com/t/still-having-problems-with-through-when-going-to-same-table/74722/2 "2006-11-07T10:55:20Z")

</div>

John K. wrote:

> I am having a problem with doing a :through that goes back to the same table.

Try this…

class User \< ActiveRecord::Base  
has\_many :spanks\_as\_spankee, :foreign\_key=\>‘spanked\_user\_id’,  
:class\_name =\> ‘Spank’  
has\_many :spanks\_as\_spanker, :foreign\_key=\>‘user\_id’, :class\_name =\>  
‘Spank’  
has\_many :spanked, :through =\> :spanks\_as\_spankee  
has\_many :was\_spanked\_by, :through =\> :spanks\_as\_spanker  
end

although, if it were my app, I would make some small association and  
column name changes as well, so that…

class User \< ActiveRecord::Base  
has\_many :spanks\_as\_spankee, :foreign\_key=\>‘spankee\_id’, :class\_name  
=\> ‘Spank’  
has\_many :spanks\_as\_spanker, :foreign\_key=\>‘spanker\_id’, :class\_name  
=\> ‘Spank’  
has\_many :spankees, :through =\> :spanks\_as\_spankee  
has\_many :spankers, :through =\> :spanks\_as\_spanker  
end

class Spank \< ActiveRecord::Base  
belongs\_to :spanker, :class\_name =\> “User”, :foreign\_key =\>  
“spanker\_id”  
belongs\_to :spankee, :class\_name =\> “User”, :foreign\_key =\>  
“spankee\_id”  
end

Note: I haven’t actually tested the above, but I’ve done similar and it  
works great. Credit goes to Josh S. for showing me how to do self  
referential :throughs a while back via his blog.  
[http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 7, 2006, 10:55am UTC](https://www.ruby-forum.com/t/still-having-problems-with-through-when-going-to-same-table/74722/3 "2006-11-07T10:55:23Z")

</div>

Jon G. wrote:

> has\_many :spanked, :through =\> :spanks\_as\_spankee  
> has\_many :spankees, :through =\> :spanks\_as\_spankee  
> works great. Credit goes to Josh S. for showing me how to do self  
> referential :throughs a while back via his blog.  
> [http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)

Whoops, got an association backwards in the alternate option above. This  
is what I meant…

class User \< ActiveRecord::Base  
has\_many :spanks\_as\_spankee, :foreign\_key=\>‘spankee\_id’, :class\_name  
=\> ‘Spank’  
has\_many :spanks\_as\_spanker, :foreign\_key=\>‘spanker\_id’, :class\_name  
=\> ‘Spank’  
has\_many :spank_ers_, :through =\> :spanks\_as\_spankee  
has\_many :spank_ees_, :through =\> :spanks\_as\_spanker  
end

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 7, 2006, 10:55am UTC](https://www.ruby-forum.com/t/still-having-problems-with-through-when-going-to-same-table/74722/4 "2006-11-07T10:55:34Z")

</div>

Worked great! Thanks Jon… but now I have a couple questions for you  
if you don’t mind :-).

1. why is :through point to a has\_many relatioship? I thought it  
usually points to an object/table.

2. If I change spankers to spanked\_by it breaks… why?

Thanks again for your help :-).

On 11/2/06, Jon G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> column name changes as well, so that…
> 
> has\_many :spankees, :through =\> :spanks\_as\_spanker  
> end
> 
> >

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 7, 2006, 10:56am UTC](https://www.ruby-forum.com/t/still-having-problems-with-through-when-going-to-same-table/74722/5 "2006-11-07T10:56:08Z")

</div>

John K. wrote:

> 

Josh S., or anyone else more familiar with how this technique works,  
can probably provide much better answers. But…

1. Yes, normally it would point directly to the join model/table.  
However, to do more complex stuff (like self referential has\_many  
:throughs), sometimes you gotta break out the magic wand and wave it  
with enthusiasm.

2. I just guessing, but maybe the ‘\_by’ suffix is getting some other  
Rails magic applied to it. Try changing it to :spanked\_bx, and see if  
that helps. Either way, someone more familiar than I with the Rails  
internals might be able to explain it.
