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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:44:38+00:00 2026-06-18T17:44:38+00:00

I have two tables for tagging so that I can attach tags to any

  • 0

I have two tables for tagging so that I can attach tags to any models, it works likes so…

There’s a tagged item join table which has a tag_id column and then two other columns for polymorphism: taggable_type and taggable_id…

class TaggedItem < ActiveRecord::Base
  attr_accessible :taggable_id, :taggable_type, :tag_id

  belongs_to :taggable, :polymorphic => true
  belongs_to :tag
end

There’s also all of the things that can have tags, for example here’s a product and image model with tags attached:

class Product < ActiveRecord::Base
  has_many :tagged_items, :as => :taggable, :dependent => :destroy
  has_many :tags, :through => :tagged_items
end

class Image < ActiveRecord::Base
  has_many :tagged_items, :as => :taggable, :dependent => :destroy
  has_many :tags, :through => :tagged_items
end

The problem is with the tag model, I can seem to get the reverse work, on the tag modle I want to have a has_many images and has_many products like so:

class Tag < ActiveRecord::Base
  has_many :tagged_items, :dependent => :destroy
  has_many :products, :through => :tagged_items
  has_many :images, :through => :tagged_items
end

This is causing an error, I was wondering how I can fix this. So the tag table works through the polymorphic tagged items table.

Any help would be much appreciated. Thanks!

Edit:

Could not find the source association(s) :product or :products in model TaggedItem. Try 'has_many :products, :through => :tagged_items, :source => <name>'. Is it one of :taggable or :tag?
  • 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-18T17:44:39+00:00Added an answer on June 18, 2026 at 5:44 pm

    The has_many :through associations in your Tag model are not able to get the source association for Product and Image from the TaggedItem Model. e.g. has_many :products, :through => :tagged_items will look for a direct association belongs_to :product in TaggedItem which in case of polymorphic association is written as belongs_to :taggable, :polymorphic => true. So for the Tag model to understand exact source of the association we need to add an option :source and its type as :source_type

    So change your Tag model associations to look like

    class Tag < ActiveRecord::Base
      has_many :tagged_items, :dependent => :destroy
      has_many :products, :through => :tagged_items, :source => :taggable, :source_type => 'Product'
      has_many :images, :through => :tagged_items, :source => :taggable, :source_type => 'Image'
    end
    

    This should fix your problem. 🙂

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

Sidebar

Related Questions

I have two tables which have a column in common, how can I retrieve
I have two tables listings and bids. I have a query that will return
I have two tables that have the same structure, I need a query to
I have two tables in MySql Company : (cname,city) works : (ename,cname,salary) I want
I have two tables in the system. I'm trying to make a VIEW that
I have two tables named utilities and types that i am trying to access
I have two tables with a foreign key constraint how can I delete rows
I have two tables that I want to use for viewing my reports which
I have two tables like Parent-Child. In Parent table, there are 7211 records. In
I have two tables: Posts and Likes And I'm trying to list the posts

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.