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

  • SEARCH
  • Home
  • 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 7715083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:17:52+00:00 2026-06-01T02:17:52+00:00

I have a problem in association between two classes, so i have a class

  • 0

I have a problem in association between two classes, so i have a class table here named Post

Class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
        t.string :post_type , null: false
        t.text :content , null: false
        t.integer :person_id
    end
    add_index :posts, :person_id
    add_index :posts, :group_id
  end
end

and the other one is called Action

class CreateActions < ActiveRecord::Migration
  def change
    create_table :actions do |t|
        t.string :target_type, null:false
            t.integer :target_id
        t.integer :upvote_count
      t.timestamps
    end
      add_index :actions,:target_id
  end
end

so the problem is i want to associate the target_is as the foreign key to the Post class so i did this

class Post < ActiveRecord::Base
    has_one :action
end
class Action < ActiveRecord::Base
    belongs_to :post , :class_name => 'Target', :foreign_key => 'target_id'
end

but is doesn’t work, which when i assign Action object to action method in Post object this error is appeared

Mysql2::Error: Unknown column 'actions.post_id' in 'where clause': SELECT  `actions`.* FROM `actions`  WHERE `actions`.`post_id` = 6 LIMIT 1

so any help?

  • 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-06-01T02:17:54+00:00Added an answer on June 1, 2026 at 2:17 am

    I suppose you are trying to apply polymorphic association. Try this out.

    class Post < ActiveRecord::Base
    has_one :action, :as => :target
    end
    
    class Action < ActiveRecord::Base
    belongs_to :target, :polymorphic => true
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem :( I have a many-many table between two tables(1&2), via
I have a has_many and belongs_to association set up between two models: Project and
I have a table called SecurityContacts and another table called Contacts. The association between
Background I have two objects which have bidirectional association between them in a C#
I have the same problem of this topic. but on the association between entities.
i have problem with autorotate on iphone i set up in all classes -
I have got the following models, class City < ActiveRecord::Base belongs_to :state belongs_to :country
I have two MySQL tables A and B . Table A has a member
I have two classes Event and Review. The event has an instance object Review
I am having problems setting up an association in entity framework between two tables

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.