Alienazk is trying to add an attribute to the User model in a Discourse plugin by linking it to a custom table (“total_points”) and accessing its column “total_earned_points”. Their initial code fails with a nil error, likely because total_point is not properly initialized or accessed. They attempted to fix it by switching from self.total_point[:total_earned_points] to self.total_point.read_attribute(:total_earned_points)in post 4, but the issue persists. pfaffman suggests checking if an expected array is nil and recommends reviewing existing plugins like discourse-calendar or the all-the-plugins repo for working examples of similar patterns. Alienazk clarifies they simply want to expose a column from a new table as an attribute on the User model. The core problem appears to be improper association or attribute access in the add_to_class block, possibly due to the total_point association not being correctly defined or loaded.
# total_points is a sum table with column "total_earned_points".
module MyModel::UserExtension
def self.prepended(base)
base.has_many :user_points, class_name: 'MyModel::UserPoint'
base.has_one :total_point, class_name: 'MyModel::TotalPoint'
end
end
reloadable_patch do |plugin|
User.class_eval { prepend MyModel::UserExtension }
end
add_to_class(:user, :total_earned_points) do
# self.user_points.sum(:reward_points)
self.total_point[:total_earned_points]
end
I am very new to rails. Could you please tell me what is wrong with it?
Thanks so much!