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?