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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:00:27+00:00 2026-05-23T05:00:27+00:00

I know this topic has been approached several times before in various ways, but

  • 0

I know this topic has been approached several times before in various ways, but I haven’t been able to synthesize the other questions to meet this particular use case for Rails 3:

I am trying to construct a data model in which there are Attachments and Attachables. Any Attachment can be connected with an Attachable, and vice-versa. I would like this to ultimately be used as a combination acts_as plugin (acts_as_attachable and acts_as_attachment).

My current schema uses an Attacher model with two polymorphic belongs_to relationships to attempt to accomplish this.

The specific case outlined here involves an Attachable model (Good) and two Attachment models (Image and Movie).

Here is what I have so far:

attacher.rb

class Attacher < ActiveRecord::Base
    belongs_to :attachment, :polymorphic => true
    belongs_to :attachable, :polymorphic => true
end

good.rb

class Good < ActiveRecord::Base
    has_many :attachment_relations, :as => :attachable, :class_name => 'Attacher'
    has_many :attached_images, :through => :attachment_relations, :source => :attachment, :source_type  => 'Image'
    has_many :attached_movies, :through => :attachment_relations, :source => :attachment, :source_type  => 'Movie'
end

image.rb

class Image < ActiveRecord::Base
    has_many :attachable_relations, :as => :attachment, :class_name => 'Attacher'
end

movie.rb

class Movie < ActiveRecord::Base
    has_many :attachable_relations, :as => :attachment, :class_name => 'Attacher'
end

This works insofar as I can do the following:

@g = Good.create!
@i = Image.create!
@m = Movie.create!

@g.attached_images << @i
@g.attached_images       # [#<Image id: 1 ... >]

@g.attached_movies << @m
@g.attached_movies       # [#<Movie id: 1 ... >]

@g.attachment_relations  # [#<Attacher id: 1, attachable_id: 1, attachable_type: "Good", attachment_id: 1, attachment_type: "Image" ...>, #<Attacher id: 2, attachable_id: 1, attachable_type: "Good", attachment_id: 1, attachment_type: "Movie" ...>]

The thrust of my question is: can I use associations to construct the following method/return so I can still do things like @g.attachments.where( ... ):

@g.attachments           # [#<Image id: 1 ... >, #<Movie id: 1 ... >]

I have tried a bunch of things like this:

good.rb

class Good < ActiveRecord::Base
    has_many :attachment_relations, :as => :attachable, :class_name => 'Attacher'
    has_many :attachments, :through => :attachment_relations
end

But they tend to throw me this:

ActiveRecord::HasManyThroughAssociationPolymorphicError: Cannot have a has_many :through association 'Good#attachments' on the polymorphic object 'Attachment#attachment'

I think I basically need something that could act like source_type => :any …

Also, this is my first question on StackOverflow, so please let me know if there is anything I can do to improve my question. Thanks in advance! 🙂

  • 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-23T05:00:27+00:00Added an answer on May 23, 2026 at 5:00 am

    Unfortunately, I have come to the conclusion that there isn’t an easy or standard way to do this. The clearest basic solution I have found is from Squeegy’s answer to this question: Rails polymorphic many to many association, where he references his gist to a reasonably simple has_relations module.

    I think this basically answers my question and serves a good jumping-off point for creating a more robust solution.

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

Sidebar

Related Questions

ok, I know that this topic has been addressed several times on here, but
First of all, I know this topic has been brought up several times before
Ok so I know this topic has many questions, but I still haven't been
I know this topic has been addressed several times here, however my problem is
I know this topic has been discussed to death, but there is one thing
I know this topic has been asked, but the posts are all out of
I know this topic has been beat to death but a lot of the
I know this topic has been beaten into the ground, but I'm really stumped.
I know a similar question on this topic has been asked, but doesn't look
Now I realise this topic has been covered many times before. However there did

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.