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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:56:14+00:00 2026-05-21T15:56:14+00:00

my problem is following. How can I joins belongs_to association from polymorphic model There

  • 0

my problem is following. How can I joins belongs_to association from polymorphic model

There is situation

opinion.rb

class Opinion < ActiveRecord::Base
    belongs_to :opinionable, :polymorphic => true
    belongs_to :category
end

answer.rb

class Answer < ActiveRecord::Base
    has_many :opinions, :as => :opinionable
end

How can i do following

Opinion.joins(:opinionabe).all

it will throw

ArgumentError: You can’t create a polymorphic belongs_to join without specifying the polymorphic class!

How can i specific which class i want to join?

Second question. How to preload it?

Opinion.preload(:opinionable).all

works fine. It will do query for each class in belongs_to.

But. if i want to do something like

Opinion.preload(:opinionable => :answer_form).all

there is problem because one model has this association and second hasn’t. So it will throw exception.

So how i can do something like

Opinion.preload(:answer => :answer_form, :another_belongs_to_model).all

?

Thanks, David!

  • 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-21T15:56:14+00:00Added an answer on May 21, 2026 at 3:56 pm

    It looks like you have not specified opinionable_type:string column for your Opinion model.

    Try to update your migration in this manner:

    class CreateOpinions < ActiveRecord::Migration
      def self.up
        create_table :opinions do |t|
          t.integer :opinionable_id
          t.string  :opinionable_type
    
          # ... other fields
    
          t.timestamps
        end
      end
    
      def self.down
        drop_table :opinions
      end
    end
    

    This will solve your second question and Opinion.preload(:opinionable).all should work well.

    You cann’t do joins on polymorphic association because they can be located in different tables, which are detected after Opinion model is loaded. That why model needs column opinionable_type.

    If you try to do this you’ll get next exception

    ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association :opinionable

    UPD: Added magic join ^_^

    class Opinion < ActiveRecord::Base
      belongs_to :opinionable, :polymorphic => true
    
      belongs_to :opinionable_answer, :foreign_key => :opinionable_id, :class_name => "Answer"
    
      scope :by_type, lambda { |type| joins("JOIN #{type.table_name} ON #{type.table_name}.id = #{Opinion.table_name}.opinionable_id AND #{Opinion.table_name}.opinionable_type = '#{type.to_s}'") }
    end
    

    Example:

    Opinion.by_type(Answer).to_sql
      => "SELECT \"opinions\".* FROM \"opinions\" JOIN answers ON answers.id = opinions.opinionable_id AND opinions.opinionable_type = 'Answer'" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following many-to-many relationship: class Assignment < ActiveRecord::Base belongs_to :programmer belongs_to :project
I have the following associations: class User < ActiveRecord::Base has_and_belongs_to_many :brands, :join_table => 'brands_users'
I have the following models in my Rails application: class Shift < ActiveRecord::Base has_many
Let's assume we have following models: from django.db import models class Foo(models.Model): name =
I have following structure: class User < ActiveRecord::Base has_many :Hobbies, :dependent => :destroy accepts_nested_attributes_for
How can I fix the following problem with IE6/7(IE Tester) with drop-down list(with fixed
I have the following problem: I have an Activity where a user can start
I have the following problem. I am making this website you can see in
The following is 2 different definitions of the problem: How can I process 2
it is the problem, i can't undertand anyway. i have the following simple script

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.