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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:20:55+00:00 2026-05-24T05:20:55+00:00

I have some problems to set up and association correctly, I looked at all

  • 0

I have some problems to set up and association correctly, I looked at all the questions asked about polymorphic association here but none seems to match my case.

Here is a minimal working test:

require 'rubygems'

gem 'activerecord', '3.0.8'

require 'active_record'
require 'mysql'

ActiveRecord::Base.establish_connection(
  :adapter => 'mysql',
  :database => 'test_db',
  :user => 'root'
)

class User < ActiveRecord::Base
  belongs_to :site

end

class Site < ActiveRecord::Base
  has_many :folders, :as => :parent
  has_many :users

end

class Folder < ActiveRecord::Base
  belongs_to :parent, :polymorphic => true
  has_many :users, :through => :parent
end


p Folder.first.users
# => NameError: uninitialized constant Folder::Parent

And here is my schema:

# inimal database schema :
# 
# create_table :sites do |t|
#   t.string      :name,              :null => false
# end
# 
# create_table :users do |t|
#   t.string      :login,         :null => false
#   t.integer     :site_id,       :null => false
# end
# 
# create_table :folders do |t|
#   t.string      :label,         :null => false
#   t.string      :parent_type,   :null => false
#   t.integer     :parent_id,     :null => false
# end

Is there any way to make this works as an association ?
For now I ended up replacing the users association with:

def users
  parent.users
end

but obviously that prevents me from using users as a standard association :/

Edit: The folder’s parent cannot be a folder itself, in this code the parent can only be a Site (it can be some other things in the real code but it works the same way).

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

    I don’t think Rails supports has_many :through passing through polymorphic associations.

    In Rails 3.1 rc 1, I get an explicit exception in the rails console:

    ruby-1.9.2-p180 :011 > p Folder.first.users
      Folder Load (0.1ms)  SELECT "folders".* FROM "folders" LIMIT 1
    ActiveRecord::HasManyThroughAssociationPolymorphicThroughError: Cannot have a 
    has_many :through association 'Folder#users' which goes through the 
    polymorphic association 'Folder#parent'.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problems with this code? Here is my code that creates text
we have here some problems with our Magento system. First the background : We
I have some problems with UITableView and sections/rows. Iam parsing the section names, all
I have some problems sending mails through SMTP using Spring's MailSender interface and the
I have some problems with OpenCV s cvCanny(...) and the Image data types it
i have some problems with rowspan: var doc1 = new Document(); doc1.SetPageSize(PageSize.A4.Rotate()); string path
I have some problems sending an id though jquery. I have a form select
I have some problems with mod_rewrited at .httacess. We have created a website, the
I have some problems to start programming using cocos2D on linux + android. I
I have some problems with controlling a while loop inside an event structure. Say

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.