Ruby on Rails - 多对一类型关联

发布于 2024-12-20 02:54:33 字数 1408 浏览 3 评论 0原文

我正在尝试创建某种多对一关联。基本前提是资金/交易流系统,用于跟踪用户的两个帐户之间(可能是钱包和支票帐户之间)的资金。

我有一个交易模型,它存储基本信息 - 从哪个帐户借记、贷记哪个帐户、金额是多少。

我还有一个帐户模型,用户可以创建多个帐户模型(可能一个用于钱包,一个用于信用卡,一个用于支票帐户等)。

我认为我遇到的问题是我的交易模型引用了帐户模型两次,一次针对credit_id,一次针对debit_id。

我正在尝试找出我需要的关联,并且我认为我需要多对一(多笔交易,一个帐户)。我认为我不需要连接表,但我不完全确定。

这是我的基本模型代码,我真的不知道从这里去哪里。

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

然后对于帐户模型:

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

通过此模型实现,我可以正确获取 transaction.credit 和 transaction.debit,但是当我尝试执行类似 account.credit_transactions 的操作时,我收到此错误:

ActiveRecord::StatementInvalid: SQLite3::SQLException: 没有这样的列: transactions.account_id: SELECT "accounts".* FROM "accounts" INNER JOIN "transactions" ON "accounts".id = "transactions".debit_id WHERE ( (“交易”.account_id = 3))

老实说,我有点不知道下一步该去哪里,所以任何帮助将不胜感激。谢谢!

[编辑:更新模型代码]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔岸观火 2024-12-27 02:54:33

听起来您的交易有一个贷方帐户和一个借方帐户?

class Transaction < ActiveRecord::Base

  has_one :credit_account, :class_name => "Account", :foreign_key => "credit_account_id"
  has_one :debit_account, :class_name => "Account", :foreign_key => "debit_account_id"

end

如果这不起作用,您能否提供更多关于模型之间的关系应如何工作的信息?

By the sounds of it your transaction has one credit account and one debit account?

class Transaction < ActiveRecord::Base

  has_one :credit_account, :class_name => "Account", :foreign_key => "credit_account_id"
  has_one :debit_account, :class_name => "Account", :foreign_key => "debit_account_id"

end

IF that doesn't work, could you give a little more info on how the relationships between your models should work?

找个人就嫁了吧 2024-12-27 02:54:33

抱歉耽搁了,但终于弄清楚了。这是我的代码

    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"

和帐户

    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'

感谢所有提供帮助的人!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文