我的Rails 4應用程序中的兩個模型是 - Accommodation.rb
和Account.rb
- 每個住宿has_one
帳戶。從其他模型調用模型
在我Accommodation.rb
我有這樣的方法:
def my_method
self.field = "Something"
save!
end
,我打電話使用從AccommodationsController.rb
方法:
accommodation.my_method
,但我也需要能夠調用一個方法在我Account.rb
從我的Accommodation.rb
模型,同時保持關聯,以便如果我創建或更新帳戶表中的記錄,它將知道哪個住宿ID參考。對東西的線路:
Account.accommodation.my_other_method
,但我知道這是行不通的,因爲它是一個「刺在黑暗中」
注意我試圖讓我的邏輯在我的模型
您可能只需爲相關記錄調用'@ accommodation.account'和'@ account.accommodation'。 –