# Custom foreign key field

**URL:** <https://www.ruby-forum.com/t/custom-foreign-key-field/48437>\
**Category:** Rails\
**Created:** [November 20, 2005, 5:08pm UTC](https://www.ruby-forum.com/t/custom-foreign-key-field/48437 "2005-11-20T17:08:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![straightflush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4bbf92/32.png) [@straightflush](https://www.ruby-forum.com/u/straightflush)\
**Post date:** [November 20, 2005, 5:08pm UTC](https://www.ruby-forum.com/t/custom-foreign-key-field/48437/1 "2005-11-20T17:08:34Z")

</div>

I have 3 tables, news,activities, threads.

threads contains all the replies for news and activities. Therefore  
threads has a category\_id and postid column to map a reply to a table  
and initial thread.

How do i say that news has many threads, but threads has a generic  
post\_id foreign key field, not a news\_id foreign key field as well as  
a category\_id of 1 (or 2 if its activities)?

thanks  
adam

---

<div class="post-metadata">

**Author:** ![straightflush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4bbf92/32.png) [@straightflush](https://www.ruby-forum.com/u/straightflush)\
**Post date:** [November 20, 2005, 8:14pm UTC](https://www.ruby-forum.com/t/custom-foreign-key-field/48437/2 "2005-11-20T20:14:59Z")

</div>

Hi Adam,

I’ll take a guess that you want to do this:

-news  
id serial  
body\_text text

-activities  
id serial  
body\_text text

-replies  
id serial  
owner\_class varchar(255)  
owner\_id int  
body\_text text

Class News \< ActiveRecord::Base  
has\_many :replies,  
:foreign\_key =\> “owner\_id”  
:conditions =\> “owner\_class = ‘News’”  
:order =\> ‘id’  
end

Class Activities \< ActiveRecord::Base  
has\_many :replies,  
:foreign\_key =\> “owner\_id”  
:conditions =\> “owner\_class = ‘Activities’”  
:order =\> ‘id’  
end

Class Replies \< ActiveRecord::Base  
end

I am doing exactly this for images in store.rb project. If someone has a  
better idea I’d like to hear it too!

Peter

---

<div class="post-metadata">

**Author:** ![straightflush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4bbf92/32.png) [@straightflush](https://www.ruby-forum.com/u/straightflush)\
**Post date:** [November 20, 2005, 8:35pm UTC](https://www.ruby-forum.com/t/custom-foreign-key-field/48437/3 "2005-11-20T20:35:06Z")

</div>

why not just use single table inheritance?

## posts

id  
type (magic column name)  
body

## replies

id  
post\_id  
body

class Post \< ActiveRecord::Base  
end

class NewsPost \< Post  
has\_many :replies  
end

class Article \< Post  
has\_many :replies  
end

class Reply \< ActiveRecord::Base  
belongs\_to :post  
end

news\_post = NewsPost.create(“foobar”)  
news\_post.replies \<\< Reply.create(“barfoo”)

post = Post.find(:first) # post == NewsPost object

---

<div class="post-metadata">

**Author:** ![straightflush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4bbf92/32.png) [@straightflush](https://www.ruby-forum.com/u/straightflush)\
**Post date:** [November 20, 2005, 8:59pm UTC](https://www.ruby-forum.com/t/custom-foreign-key-field/48437/4 "2005-11-20T20:59:16Z")

</div>

Could this all be in just one table since posts and replies are so  
similar?  
I haven’t used single table inheritance before but maybe something like  
this  
will work?

## posts

id  
type (magic column name: News, Article, Reply)  
body  
parent\_id

class Post \< ActiveRecord::Base  
acts\_as\_tree  
end

class News \< Post  
end

class Article \< Post  
end

class Reply \< Post  
end

A new thread (either News or Article) will have parent\_id NULL.
