HAS_ONE:通过=>多 [英] has_one :through => multiple
问题描述
这两个Attendment和放大器;华信:
Both Attendment & Vouching:
belongs_to :event
belongs_to :account
因此:1至1 attendments和vouchings关系
Therefore: 1 to 1 relationship between attendments and vouchings.
有没有办法做到这一点没有我想得太多了?
Is there a way to do this without my thinking too much?
# attendment
has_one :vouching :through => [:event, :account]
注:我不介意想太多,实际上
Note: I don't mind thinking too much, actually.
推荐答案
是啊,我不认为你可以使用一个HAS_ONE这一点。假设我正确读这篇文章,你有两种模式:
Yeah i don't think you can use a has_one for this. Assuming I'm reading this correctly, you have two models:
Attendment 华信
Attendment Vouching
他们都存储一个EVENT_ID和ACCOUNT_ID。你想知道从attendment模型,什么华信股份相同的事件和帐户作为attendment。我认为这是最简单的解决方法是写你的attendment.rb文件中的方法。
They both store an event_id and account_id. You want to know from the attendment model, what vouching shares the same event and account as the attendment. I think the easiest solution for this is to write a method inside your attendment.rb file.
class Attendment < ActiveRecord::Base
# belong to statements go here
def voucher
Voucher.where(:event_id => self.event_id, :account_id => self.account_id).first
end
end
这篇关于HAS_ONE:通过=&GT;多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!