我正在通過rails教程,並遇到此代碼。我無法理解這種情況下的自我關鍵字可能會做什麼。根據我的理解,self關鍵字是一種創建屬於其創建對象的類函數的方法,但我真的不明白這是什麼意思。軌道協會的上下文中的self關鍵字
的代碼如下:
class User < ActiveRecord::Base
def feed
end
def following?(other_user)
self.relationships.find_by(followed_id: other_user.id)
end
def follow!(other_user)
self.relationships.create!(followed_id: other_user.id)
end
end
誰能給這個一個相當直觀的解釋?我熟悉其他語言的「this」關鍵字,但這看起來像是鐵軌魔術。謝謝!
這是一個Ruby的東西,而不是Rails。 – jcm