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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:42:13+00:00 2026-06-16T01:42:13+00:00

I need a player to have many structures and the structure to belong to

  • 0

I need a player to have many structures and the structure to belong to the player. Structure is a polymorphic relationship.

class Player < ActiveRecord::Base
  has_many :player_structures
  has_many :structures, :through => player_structures
end

class PlayerStructures < ActiveRecord::Base
  belongs_to :structure, polymorphic: true
  belongs_to :player
end

class StructureA < ActiveRecord::Base
  has_one :player_structure, :as => :structure
  has_one :player, :through => :player_structure
end

class StructureB < ActiveRecord::Base
  has_one :player_structure, :as => :structure
  has_one :player, :through => :player_structure
end

But if I pull out Player.first and ask for its structures, it gives:

ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'Player#structures' on the polymorphic object 'Structure#structure'.

But it should be able to generate a SQL query where it finds all player_structures with its id, then fetches the structure based on the structure_id and structure_type. Why does this fail and how can I validly construct a polymorphic join table?

UPDATE

If I do what I want it to do manually, it works:

player_structures.collect(&:structure)

Rails, y u no do that?

  • 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-16T01:42:15+00:00Added an answer on June 16, 2026 at 1:42 am

    I think you need to be more specific in defining your relationships in your Player model. For example:

    class Player < ActiveRecord::Base
      has_many :player_structures
      has_many :structureas, :through => player_structures, :source => :structure, :source_type => 'StructureA'
      has_many :structurebs, :through => player_structures, :source => :structure, :source_type => 'StructureB'
    end
    

    Then you can make a method that’ll return all the structures defined in the relationships instead of having to access each one individually.

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

Sidebar

Related Questions

I have the following models set up: class Player < ActiveRecord::Base has_many :game_players has_many
I have the following models: school.rb class School < ActiveRecord::Base has_many :offers has_many :players,
I have a entity Player that has one to many association to other entities.
I have 2 models ( player and team linked through the model lnkteamplayer )
i have a little problem with the media player class. I have a button,
I have a DB with players and events. One player has many events. Event
I am trying to have a many to many relationship between players and fixtures.
I have a page with multiple YouTube embedded players that I need to listen
I need to embed the Flash player in a native application (C++) in a
What precisely do I need to make a multi-player game on smartphones like Blackberry

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.