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 8206355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:38:05+00:00 2026-06-07T08:38:05+00:00

In my project I need User to be able to both create/own its own

  • 0

In my project I need User to be able to both “create/own” its own objects and “track” (or “follow”) objects created by other users. Here is my approach:

class User < ActiveRecord::Base
  has_many :widgets
  has_many :gadgets
  has_many :nuggets

  has_many :tracks

  has_many :tracked_widgets, :as => :trackable, :trackable_type => "Widget"
  has_many :tracked_gadgets, :as => :trackable, :trackable_type => "Gadget"
  has_many :tracked_nuggets, :as => :trackable, :trackable_type => "Nugget"
end

class Track < ActiveRecord::Base
  belongs_to :trackable, polymorphic: true
  belongs_to :user
end

class Widget < ActiveRecord::Base  # Gadget and Nugget classes are the same
  has_many :tracks, :as => :trackable
end

The schema for the tracks table:

  create_table "tracks", :force => true do |t|
    t.integer  "user_id"
    t.integer  "trackable_id"
    t.string   "trackable_type"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "tracks", ["trackable_id", "user_id"], :name => "index_tracks_on_trackable_id_and_user_id"

With this implementation I get the following error:

Unknown key: trackable_type (ArgumentError)

Looking at the schema, it’s obvious that I don’t have :trackable_type set up as an index, so the error message makes sense. However, in the examples I’ve seen that demonstrate polymorphic relationships, I’ve never seen the ______able_type field set up as an index. This makes me wonder if I’m missing something basic here. I have these questions:

  1. Have I somehow incorrectly set up the has_many and :as => :trackable relationships?
  2. If not, is there any issue using a string field as an index?
  3. If the answer is simply to add the :trackable_type field as an index, what is the best way to do this after the fact? Add a new migration that creates the :trackable_type index?
  • 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-07T08:38:06+00:00Added an answer on June 7, 2026 at 8:38 am

    I finally figured out how to make the polymorphic association work for this case. Not sure I understand why, but the :as option for has_many is not used in some polymorphic cases. Instead you must use a standard :through option and then reestablish the polymorphism with the :source and :source_type options.

    In my example above, the has_many statements in the User model the needed to be formatted as follows:

    has_many :tracked_widgets, :through => :tracks, :source => :trackable, :source_type => "Widget"
    has_many :tracked_gadgets, :through => :tracks, :source => :trackable, :source_type => "Gadget"
    has_many :tracked_nuggets, :through => :tracks, :source => :trackable, :source_type => "Nugget"
    

    Hopes this helps anyone trying to address a similar situation.

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

Sidebar

Related Questions

In my project i need to take an expression input from user as string
I am doing a project and need to use the facebook API as user
I am working on a project and I need to save user configuration. The
I have a Middleman project and I need to emulate a logged-in user. Simple
I'm doing a project in Python (3.2) for which I need to compare user
i have a job portal website project in that i need to signup user
I'm working on a Rails project and I'm in need of a user-friendly WYSIWYG.
Do we need to manually import in main project the user location graphical assets
In my project I take a string from user and then I need to
in the project I'm creating I need to check if the user is logged

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.