【问题标题】:has_one :through => multiplehas_one :through => 多个
【发布时间】:2011-08-08 00:46:11
【问题描述】:
出席和证明:
belongs_to :event
belongs_to :account
因此:出席和担保之间是一对一的关系。
有没有办法不用我想太多?
# attendment
has_one :vouching :through => [:event, :account]
注意:其实我不介意想太多。
【问题讨论】:
标签:
ruby-on-rails
activerecord
associations
has-many-through
has-one
【解决方案1】:
是的,我认为您不能为此使用 has_one。假设我没看错,你有两个模型:
出席
担保
它们都存储 event_id 和 account_id。您想从出席模型中知道,哪些凭证与出席共享相同的事件和帐户。我认为最简单的解决方案是在您的attendment.rb 文件中编写一个方法。
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