Ruby on Rails - 多对一类型关联
我正在尝试创建某种多对一关联。基本前提是资金/交易流系统,用于跟踪用户的两个帐户之间(可能是钱包和支票帐户之间)的资金。
我有一个交易模型,它存储基本信息 - 从哪个帐户借记、贷记哪个帐户、金额是多少。
我还有一个帐户模型,用户可以创建多个帐户模型(可能一个用于钱包,一个用于信用卡,一个用于支票帐户等)。
我认为我遇到的问题是我的交易模型引用了帐户模型两次,一次针对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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您的交易有一个贷方帐户和一个借方帐户?
如果这不起作用,您能否提供更多关于模型之间的关系应如何工作的信息?
By the sounds of it your transaction has one credit account and one debit account?
IF that doesn't work, could you give a little more info on how the relationships between your models should work?
抱歉耽搁了,但终于弄清楚了。这是我的代码
和帐户
感谢所有提供帮助的人!
Sorry for the delay, but finally figured it out. Here's my code
and for account
Thanks all who helped!