我在查看一些代碼,我看到delegate
和has_many + through
被用來代替設置一個附加的關聯。我想知道使用delegate
的好處在於設置另一個has_many
關聯。使用委託而不是在Rails中構建關聯。重點是什麼?
class AppleCore
belongs_to :apple
delegate :tree to :apple
end
然後
class Apple
belongs_to :tree
end
最後
class Tree
has_many :apples
end
在這個例子......爲什麼我們只是委託樹的蘋果,而不是創造AppleCore與樹之間的關聯?