3

出席和担保:

belongs_to :event
belongs_to :account

因此:出勤和凭证之间是一对一的关系。

有没有办法不用我想太多来做到这一点?

# attendment
has_one :vouching :through => [:event, :account]

笔记:其实我不介意想太多。

4

1 回答 1

2

是的,我认为您不能为此使用 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
于 2011-08-08T02:32:07.577 回答