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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:42:28+00:00 2026-05-27T20:42:28+00:00

I’m trying to create some sort of many-to-one association. The basic premise is a

  • 0

I’m trying to create some sort of many-to-one association. The basic premise is a money/transaction flow system to keep track of money between a user’s two accounts (perhaps between a wallet and a checking account).

I have a Transaction model, which stores the basic information – what account to debit from, what account to credit to, what the amount is.

I also have an Account model, which the user can create multiple ones of (maybe one for Wallet, one for Credit Card, one for Checkings Account, etc).

The problem I think I’m running into is that my Transaction model references the Account model twice, once for a credit_id and once for a debit_id.

I’m trying to figure out the association I need, and I think I need a many-to-one (many transactions, one account). I don’t think I need a join table, but I’m not entirely sure.

Here’s my basic model code, I’m really not sure where to go from here.

class Transaction < ActiveRecord::Base
  attr_accessible :amount, :description, :credit_id, :debit_id

  belongs_to :user

  belongs_to :debit, :class_name => "Account"
  belongs_to :credit, :class_name => "Account"


end

And then for Account model:

class Account < ActiveRecord::Base
  attr_accessible :name, :credit_transactions, :debit_transactions

  belongs_to :user

  has_many :transactions
  has_many :credit_transactions, :through => :transactions, :source => :credit
  has_many :debit_transactions, :through => :transactions, :source => :debit
end

With this model implementation, I can get transaction.credit and transaction.debit correctly, but when I try do something like account.credit_transactions, I get this error:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: transactions.account_id: SELECT “accounts”.* FROM “accounts” INNER JOIN “transactions” ON “accounts”.id = “transactions”.debit_id WHERE ((“transactions”.account_id = 3))

I’m honestly kinda stuck about where to go next, so any help would be appreciated. Thanks!

[edit: updated model codes]

  • 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-27T20:42:28+00:00Added an answer on May 27, 2026 at 8:42 pm

    Sorry for the delay, but finally figured it out. Here’s my code

        class Transaction < ActiveRecord::Base
          attr_accessible :amount, :description, :long_description, :credited_id, :debitted_id, :custom_credit, :custom_debit
    
          belongs_to :user
    
          belongs_to :debitted, :class_name => "Account"
          belongs_to :credited, :class_name => "Account"
    

    and for account

        class Account < ActiveRecord::Base
          attr_accessible :name, :debit_shorthand, :credit_shorthand, :credit_transactions, :debit_transactions
    
          belongs_to :user
    
          has_many :credit_transactions, :class_name => "Transaction", :foreign_key => 'credited_id'
          has_many :debit_transactions, :class_name => "Transaction", :foreign_key => 'debitted_id'
    

    Thanks all who helped!

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.