我在Ruby on Rails的新的,對下面的代碼有點混亂Rails和Ruby類,實例變量
module Comment
class CommentScore < ActiveRecord::Base
self.table_name = 'comment_scores'
# Associations
belongs_to :provider_account
# Scopes
scope :by_provider_account_id, lambda { |provider_account_id| where(provider_account_id: provider_account_id) }
# Instance methods
def set
return unless self.valid?
return if unsettable?
self.positive_count = provider_account.comment.total(:positive)
self.total = provider_account.comment.total(:all)
self.score = decimal
self.save!
end
def decimal
positive_count.to_d/total.to_d
end
end
end
從我的研究
,我知道,如果「自我」關鍵字內部使用一種方法,它只是實際調用的方法就像這個例子,self.positive_count,調用該方法「positive_count」這軌,意味着名爲「positive_count」表列。
所以我在'十進制'方法有點困惑,它只是使用'positive_count'沒有自我?這不是一個局部變量嗎?
簡潔明瞭。不知道activerecord會自動定義getter和setter。當我讀到這一切都有道理。謝謝。 – Finks