1
我有一個名爲CommonApp模型,並在其上的功能,產生的進度%的基礎上,如何有多少用戶已經填寫。在每列節省模式,節省的進度值
你可以在這裏查看進度功能 - >
def progress
total_questions = self.attribute_names.count + 2
# +4 for name, cities,industries,positions, associated models
# -4 for id, created_at, updated_at, user_id these will always be filled anyways
# +2 for video, associated model, but weighed more heavily
total_completed = 0
self.attribute_names.each do |attr|
total_completed += 1 unless self[attr].blank?
end
total_completed = total_completed - 3
# +1 name (on user table, not on here
# -4 for id, created_at, updated_at, user_id
total_completed += 1 if self.cities.any?
total_completed += 1 if self.positions.any?
total_completed += 1 if self.industries.any?
total_completed += 2 if self.user.video
(100.0*total_completed/total_questions).round
end
我想以後排序的進步,所以這意味着,我認爲做到這一點的最好辦法是有一個列CommonApp叫進步,更新任何時候進度改變時,該列的值。
我該怎麼做?
更新 我正在考慮更多沿着下面的行,這將更新每次進度函數運行,並與進度欄的值不同。但是,我得到的錯誤 -
undefined method `save!' for 93:Fixnum
這是最新進展功能
def progress
total_questions = self.attribute_names.count + 2
# +4 for name, cities,industries,positions
# -4 for id, created_at, updated_at, user_id
# +2 for video
total_completed = 0
self.attribute_names.each do |attr|
total_completed += 1 unless self[attr].blank?
end
total_completed = total_completed - 3
# +1 name
# -4 for id, created_at, updated_at, user_id
total_completed += 1 if self.cities.any?
total_completed += 1 if self.positions.any?
total_completed += 1 if self.industries.any?
total_completed += 2 if self.user.video
value = (100.0*total_completed/total_questions).round
unless self.progress_level == value
self.progress_level = value
self.save!
end
(100.0*total_completed/total_questions).round
end
您可以使用'before_action'過濾器來檢查參數並相應地追加/更改進度參數(?)。 – rlecaro2
您可以使用Rails的觀察器模塊觀察增加/減少CommonApp進度的對象,並更新數據庫中的相關進度值。 – MrYoshiji
@MrYoshiji觀察者在Rails 4.0中正式被棄用。對? –