# Polymorphic association with legacy database

**URL:** <https://www.ruby-forum.com/t/polymorphic-association-with-legacy-database/208708>\
**Category:** Rails\
**Created:** [June 24, 2011, 11:51pm UTC](https://www.ruby-forum.com/t/polymorphic-association-with-legacy-database/208708 "2011-06-24T23:51:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [June 24, 2011, 11:51pm UTC](https://www.ruby-forum.com/t/polymorphic-association-with-legacy-database/208708/1 "2011-06-24T23:51:20Z")

</div>

Let’s say I have a legacy table called server with serverid:integer,  
servername:string and I created a new table called action that will  
have:

t.integer :actionable\_id  
t.string :actionable\_type  
t.string :text

And I want this table to have a poly relationship with the legacy server  
table.

When I do this:

class Server \< ActiveRecord::Base  
set\_table\_name “server”  
set\_primary\_key “serverid”

has\_many :actions, :as =\> :actionable, :foreign\_key =\> ‘serverid’  
end

class Action \< ActiveRecord::Base  
belongs\_to :actionable, :polymorphic =\> true  
end

And I try to get the Server for a given Action:

ruby-1.9.2-p180-patched :013 \> Action.first.actionable

I get this error:

Mysql2::Error: Unknown column ‘server.id’ in ‘where clause’: SELECT  
`server`.\* FROM `server` WHERE `server`.`id` = 10 LIMIT 1  
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column  
‘server.id’ in ‘where clause’: SELECT `server`.\* FROM `server` WHERE  
`server`.`id` = 10 LIMIT 1

How can I make the Action be aware that the primary key is ‘serverid’  
and not ‘id’, the same way it is aware that the table name here is  
‘server’ and not ‘servers’? Shouldn’t it use the set\_primary\_key value?

Is this a Polymorphic relationship bug?
