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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:50:59+00:00 2026-05-16T08:50:59+00:00

class Transaction < ActiveRecord::Base belongs_to :account, :polymorphic => true end class Bankaccount < ActiveRecord::Base

  • 0
class Transaction < ActiveRecord::Base
  belongs_to :account, :polymorphic => true
end

class Bankaccount < ActiveRecord::Base
  has_many :transactions, :as => :account
end

class Creditcard < ActiveRecord::Base
  has_many :transactions, :as => :account
end

Trying to do a summation of transactions where the account is active.

Transaction.sum(:all, :conditions => "account.status = 'active'", :include => :account)

So after some reading I came across this:
The reason is that the parent model‘s type is a column value so its corresponding table name cannot be put in the FROM/JOIN clauses of that query.
The table name is bankaccounts and creditcards so does that mean they should be singular?
Also the account_type is a string either Bankaccount or Creditcard to reflect the model but should it be the tablename instead?

  • 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-16T08:51:00+00:00Added an answer on May 16, 2026 at 8:51 am

    There are two issues here:

    1. Summing over a polymorphic association.
    2. Condition over a polymorphic association.

    You can’t actually do either of these things. So you should get the same error by performing these two queries:

    1. Transactions.count(:all, :joins => :account)
    2. Transactions.find(:all, :conditions => “accounts.status = ‘active'”, :joins => :account)

    To actually get the information you need you must explicitly list out the possible parent polymorphic associations. One way to do this is to simply use SQL and LEFT JOINS, so that you can use a single query. Using Rails this can be performed with two queries:

    Creditcard.sum(
      :all, 
      :select => "transactions.amount", 
      :conditions => "creditcards.status = 'active'", 
      :joins => :transaction
    ) + Bankaccount.sum(
      :all, 
      :select => "transactions.amount", 
      :conditions => "bankaccounts.status = 'active'", 
      :joins => :transaction
    )
    

    P.S: It’s best to use :join instead of :include if you don’t plan on accessing the joined objects after the query.

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

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The easy way to fully reset a Django database is… May 16, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer Have you tried using DoEvents? While(True) If(myObject.DataReady) 'your code here… May 16, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer For Layouts First, you should start off by reading the… May 16, 2026 at 4:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have 4 models: transac, transac_data, item, dvd_details class Transac < ActiveRecord::Base has_many :transac_datas
My question involves a few different active records class Respondent < ActiveRecord::Base has_many :phone_numbers,
I have a BankAccount model class that contains data like account number, bank name,
I am trying to implement an audit trail into my application, but because of
I currently have a NHibernateHelper class which creates a session factory. I keep recieving
I am getting an InvalidOperationException('DeferRefresh' is not allowed during an AddNew or EditItem transaction.)
We're using SQLAlchemy declarative base and I have a method that I want isolate
I have a field with an unique constraint: @Column(unique=true) private String uriTitle; When I
I'm just getting rolling with RoR so I'm sure this is pretty basic. Let's
I am creating a site in Ruby on Rails, I have two models a

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.