# How change :foreign\_key on :through

**URL:** <https://www.ruby-forum.com/t/how-change-foreign-key-on-through/172749>\
**Category:** Rails\
**Created:** [July 27, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/how-change-foreign-key-on-through/172749 "2009-07-27T12:54:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hiphapis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hiphapis](https://www.ruby-forum.com/u/hiphapis)\
**Post date:** [July 27, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/how-change-foreign-key-on-through/172749/1 "2009-07-27T12:54:13Z")

</div>

Hi Guys.  
(I’m Asian. so my eng is very poor. sorry!)

i want to change :foreign\_key on has\_many :through  
code is

has\_many :following\_members, :class\_name =\> “Follow”, :dependent  
=\> :destroy  
has\_many :followings, :foreign\_key =\> “target\_id”, :through  
=\> :following\_members, :source =\> :user

but return is don’t used foreign\_key(target\_id)

i think… solution is

1. use “find\_sql”
2. def followings  
but i don’t like these.

---

<div class="post-metadata">

**Author:** ![hiphapis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hiphapis](https://www.ruby-forum.com/u/hiphapis)\
**Post date:** [July 28, 2009, 5:11pm UTC](https://www.ruby-forum.com/t/how-change-foreign-key-on-through/172749/2 "2009-07-28T17:11:03Z")

</div>

It’s going to be hard to give advice without the source to the Follow  
model - that will determine what you need to specify in  
has\_many :through.

–Matt J.

---

<div class="post-metadata">

**Author:** ![hiphapis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hiphapis](https://www.ruby-forum.com/u/hiphapis)\
**Post date:** [July 29, 2009, 2:50am UTC](https://www.ruby-forum.com/t/how-change-foreign-key-on-through/172749/3 "2009-07-29T02:50:34Z")

</div>

ohoo, really? ;(

i solved by “find\_sql”  
i don’t like it ;p

---

<div class="post-metadata">

**Author:** ![hiphapis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hiphapis](https://www.ruby-forum.com/u/hiphapis)\
**Post date:** [July 30, 2009, 7:16am UTC](https://www.ruby-forum.com/t/how-change-foreign-key-on-through/172749/4 "2009-07-30T07:16:54Z")

</div>

i finded solution!

#Follow Model  
belongs\_to :target, :foreign\_key =\> ‘target\_id’, :class\_name =\> ‘User’

#User Model  
has\_many :followings, :foreign\_key =\> “target\_id”, :through  
=\> :following\_members, :source =\> :target
