1

兩個Attendment &單證員:HAS_ONE:通過=>多

belongs_to :event 
belongs_to :account 

因此:1 attendments和vouchings之間對1的關係。

有沒有辦法做到這一點,而不是我的想法太多?

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

注意:其實我不介意太想。

回答

1

是的,我不認爲你可以使用has_one這個。假設我在正確讀這篇文章,你有兩種型號:

Attendment 華信

他們都儲存的事項標識和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