# Why through and foreign\_key not working?

**URL:** <https://www.ruby-forum.com/t/why-through-and-foreign-key-not-working/193532>\
**Category:** Rails\
**Created:** [August 19, 2010, 11:17am UTC](https://www.ruby-forum.com/t/why-through-and-foreign-key-not-working/193532 "2010-08-19T11:17:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mimosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecd19e/32.png) [@mimosa](https://www.ruby-forum.com/u/mimosa)\
**Post date:** [August 19, 2010, 11:17am UTC](https://www.ruby-forum.com/t/why-through-and-foreign-key-not-working/193532/1 "2010-08-19T11:17:14Z")

</div>

I need SQL :

SELECT `activities`.\* FROM `activities` INNER JOIN `friendships` ON  
`activities`.user\_id = `friendships`.friend\_id WHERE  
`friendships`.user\_id = 1

My User Model code:

has\_many :friendships, :dependent =\> :destroy  
has\_many :friends, :through =\> :friendships  
has\_many :activities  
##############################  
has\_many :friends\_activities, :foreign\_key =\> :user\_id, :class\_name =\>  
‘Activity’, :through =\> :friendships, :source =\> :friend

Rails3 return:

ON `activities`.id = `friendships`.friend\_id ??! :foreign\_key is  
ignored

Why !!!

---

<div class="post-metadata">

**Author:** ![mimosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecd19e/32.png) [@mimosa](https://www.ruby-forum.com/u/mimosa)\
**Post date:** [August 19, 2010, 12:29pm UTC](https://www.ruby-forum.com/t/why-through-and-foreign-key-not-working/193532/2 "2010-08-19T12:29:38Z")

</div>

On Aug 19, 10:17 am, Howl W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :activities  
> ##############################  
> has\_many :friends\_activities, :foreign\_key =\> :user\_id, :class\_name =\>  
> ‘Activity’, :through =\> :friendships, :source =\> :friend
> 
> Rails3 return:
> 
> ON `activities`.id = `friendships`.friend\_id ??! :foreign\_key is  
> ignored
> 
> Why !!!

With has many through you use the :source option to tell it which of  
the associations of friendships to follow to get to the desired row(s)

Fred

---

<div class="post-metadata">

**Author:** ![mimosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecd19e/32.png) [@mimosa](https://www.ruby-forum.com/u/mimosa)\
**Post date:** [August 20, 2010, 4:31am UTC](https://www.ruby-forum.com/t/why-through-and-foreign-key-not-working/193532/3 "2010-08-20T04:31:11Z")

</div>

Frederick C. wrote:

> With has many through you use the :source option to tell it which of  
> the associations of friendships to follow to get to the desired row(s)
> 
> Fred

Now~ I need `activities`.user\_id not is `activities`.id 😃

---

<div class="post-metadata">

**Author:** ![mimosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecd19e/32.png) [@mimosa](https://www.ruby-forum.com/u/mimosa)\
**Post date:** [August 20, 2010, 11:03am UTC](https://www.ruby-forum.com/t/why-through-and-foreign-key-not-working/193532/4 "2010-08-20T11:03:12Z")

</div>

VTD-XML 2.9, the next generation XML Processing API for SOA and Cloud  
computing, has been released. Please visit  
[VTD-XML: The Future of XML Processing - Browse Files at SourceForge.net](https://sourceforge.net/projects/vtd-xml/files/) to download the latest  
version.  
a… Strict Conformance  
a… VTD-XML now fully conforms to XML namespace 1.0 spec  
b… Performance Improvement  
a… Significantly improved parsing performance for small XML files  
c… Expand Core VTD-XML API  
a… Adds getPrefixString(), and toNormalizedString2()  
d… Cutting/Splitting  
a… Adds getSiblingElementFragment()  
e… A number of bug fixes and code enhancement including:  
a… Fixes a bug for reading very large XML documents on some  
platforms  
b… Fixes a bug in parsing processing instruction  
c… Fixes a bug in outputAndReparse()

---

<div class="post-metadata">

**Author:** ![mimosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecd19e/32.png) [@mimosa](https://www.ruby-forum.com/u/mimosa)\
**Post date:** [August 20, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/why-through-and-foreign-key-not-working/193532/5 "2010-08-20T16:38:37Z")

</div>

Jimmy Zhang wrote:

> VTD-XML 2.9, the next generation XML Processing API for SOA and Cloud  
> computing, has been released.  
> […]

Please don’t hijack threads. And this is off topic anyway.

## Best,

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

---

<div class="post-metadata">

**Author:** ![mimosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecd19e/32.png) [@mimosa](https://www.ruby-forum.com/u/mimosa)\
**Post date:** [August 20, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/why-through-and-foreign-key-not-working/193532/6 "2010-08-20T16:48:35Z")

</div>

On Aug 20, 3:31 am, Howl W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Frederick C. wrote:
> 
> > With has many through you use the :source option to tell it which of  
> > the associations of friendships to follow to get to the desired row(s)
> 
> > Fred
> 
> Now~ I need `activities`.user\_id not is `activities`.id 😃

Then you need to change the source association to point at what you  
want (i think belongs\_to has a primary\_key option these days)

Fred
