Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7603213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:36:28+00:00 2026-05-30T23:36:28+00:00

Let’s say I have a legacy MySQL table called server with fields serverid:integer and

  • 0

Let’s say I have a legacy MySQL table called server with fields serverid:integer and
servername:string.

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?

EDIT: After applying the change below (adding primary_key), it works for the Server relation, but I am sure it will fail for the other relations.

class Action < ActiveRecord::Base
  belongs_to :actionable, :polymorphic => true, :primary_key => 'serverid'
end

IMO, the value of primary_key should be automatically recognized by the target’s primary_key.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:36:29+00:00Added an answer on May 30, 2026 at 11:36 pm

    Kolrie,

    We had the same problem at my shop. Our solution was pretty straight forward. We simply defined a method that was named the same as the polymorphic relationship. So in your case you would define a method called actionable and do the following:

    def actionable
      self.actionable_type.constantize.find(self.actionable_id)
    end
    

    Not sure if that’d work for you, but it’s a good work around until the Rails app is fixed to support overriding primary_keys while using polymorphic relationships.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have the string: hello world; some random text; foo; How could
Let's say I have table with column 'URL' whrere I store urls like this
Let's say I have an image called hello.png with dimensions 200x100 . I create
Let's say we have the following table: CREATE TABLE T ( ID INT, String1
Let's say I'm building a data access layer for an application. Typically I have
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.