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

  • Home
  • SEARCH
  • 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 367567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:44:26+00:00 2026-05-12T13:44:26+00:00

Here’s my situation. I have two tables: pledges and pledge_transactions. When a user makes

  • 0

Here’s my situation. I have two tables: pledges and pledge_transactions. When a user makes a pledge, he has only a row in the pledges table.

Later when it comes time to fulfill the pledge, each payment is logged in my pledge_transactions table.

I need to be able to query all open pledges which means that the sum of the amounts in the transactions table is less than the pledged amount.

Here’s what I have so far:

named_scope :open,
   :group => 'pledges.id', 
   :include => :transactions, 
   :select => 'pledge_transactions.*', 
   :conditions => 'pledge_transactions.id is not null or pledge_transactions.id is null',
   :having => 'sum(pledge_transactions.amount) < pledges.amount or sum(pledge_transactions.amount) is null'

You might be asking yourself why I have that superfluous and ridiculous conditions option specified. The answer is that when I don’t force ActiveRecord to acknowledge the pledge_transactions table in the conditions, it omits it completely, which means my having clause becomes meaningless.

My belief is that I have run into a shortcoming of ActiveRecord.

Ultimately I need to be able to do the following:

  • Pledge.open
  • Pledge.open.count
  • Pledge.open.find(:all, …)
  • etc.

Anybody have a more elegant answer to this problem? Please no suggestions of incrementing a pledges amount_given field each time a transaction occurs. That feels like a band-aid approach and I’m much more of a fan of keeping the pledge static after it is created and computing the difference.

If I weren’t using Rails here, I’d just create a view and be done with it.

Thanks!

  • 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-12T13:44:26+00:00Added an answer on May 12, 2026 at 1:44 pm

    How is the :transactions association defined? Does it stipulate :class_name = 'PledgeTransaction' (or whatever the class is, if it uses set_table_name)?

    Have you looked at the :joins parameter? I think it might be what you were looking for. Certainly that :conditions thing doesn’t look right.

    If I weren’t using Rails here, I’d just create a view and be done with it

    Just because it’s Rails doesn’t mean you can’t use a view. OK, depending on the way it’s constructed you may not be able to update it, but otherwise go for it. You can create and drop views in migrations, too:

    class CreateReallyUsefulView < ActiveRecord::Migration
    def self.up
        # this is Oracle, I don't know if CREATE OR REPLACE is widely-supported
        sql = %{
          CREATE OR REPLACE VIEW really_usefuls AS
          SELECT
          ... blah blah SQL blah
        }
        execute sql
      end
    
      def self.down
        execute 'drop view really_usefuls'
      end
    end
    
    class ReallyUseful < ActiveRecord::Base
        # all the usual stuff here, considering overriding the C, U and D parts 
        # of CRUD if it's supposed to be read-only and you're paranoid
    end
    

    I think the books/docs don’t go into this much because implementation of, and support for views varies significantly across platforms.

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

Sidebar

Ask A Question

Stats

  • Questions 246k
  • Answers 247k
  • 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 No. It is not thread safe. the client must be… May 13, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer There are two types of solutions: First you can do… May 13, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer string masterdin = (@"K:\Drams\Cluse\" +"\\"+"Aia_Gn"+"\\"+Environment.UserName+"\\"+textBox1.Text); if(Directory.Exists(masterdin)) { MessageBox.Show("This Export set… May 13, 2026 at 8:34 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form

Trending Tags

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

Top Members

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.