merit-gem / merit

Reputation engine for Rails apps
Other
1.53k stars 198 forks source link

Adding points doesn't work the first time #341

Closed gabrielguerin closed 4 years ago

gabrielguerin commented 4 years ago

Hello,

Whenever users add their first post, they don't receive any points. However, it does work the second time and so on.

Here is my file point_rules:

    def initialize
      # New user
      score 10,
            on: 'users/confirmations#show',

            model_name: 'User',

            to: :itself

      # If user adds a post
      score 50,
            to: :action_user,

            on: 'posts#create'

      # If user downloads a file
      score -50,
            to: :action_user,

            on: 'active_storage/blobs#show'
    end

Here is the trace when I add the first post:

  CACHE Post Load (0.0ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2  [["id", 1234], ["LIMIT", 1]]
  CACHE User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 253], ["LIMIT", 1]]
  Merit::Sash Load (0.7ms)  SELECT "sashes".* FROM "sashes" WHERE "sashes"."id" = $1 LIMIT $2
  [["id", 26], ["LIMIT", 1]]
  Merit::Score Load (0.4ms)  SELECT "merit_scores".* FROM "merit_scores" WHERE "merit_scores"."sash_id" = $1 AND "merit_scores"."category" = $2 ORDER BY "merit_scores"."id" ASC LIMIT $3  [["sash_id", 26], ["category", "default"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Merit::Score::Point Create (0.4ms)  INSERT INTO "merit_score_points" ("score_id", "num_points", "created_at") VALUES ($1, $2, $3) RETURNING "id"  [["score_id", 33], ["num_points", 50], ["created_at", "2020-04-15 15:50:51.696969"]]
   (0.4ms)  COMMIT
   (0.2ms)  BEGIN
  Merit::Action Load (0.3ms)  SELECT "merit_actions".* FROM "merit_actions" WHERE "merit_actions"."id" = $1 LIMIT $2  [["id", 254], ["LIMIT", 1]]
  Merit::ActivityLog Create (0.4ms)  INSERT INTO "merit_activity_logs" ("action_id", "related_change_type", "related_change_id", "description", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["action_id", 254], ["related_change_type", "Merit::Score::Point"], ["related_change_id", 61], ["description", "a obtenu 50 points"], ["created_at", "2020-04-15 15:50:51.708060"]]
   (0.3ms)  COMMIT

Here is the trace when I add the second post:

  CACHE Post Load (0.0ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2  [["id", 1235], ["LIMIT", 1]]
  CACHE User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 253], ["LIMIT", 1]]
  Merit::Sash Load (0.3ms)  SELECT "sashes".* FROM "sashes" WHERE "sashes"."id" = $1 LIMIT $2
  [["id", 26], ["LIMIT", 1]]
  Merit::Score Load (0.4ms)  SELECT "merit_scores".* FROM "merit_scores" WHERE "merit_scores"."sash_id" = $1 AND "merit_scores"."category" = $2 ORDER BY "merit_scores"."id" ASC LIMIT $3  [["sash_id", 26], ["category", "default"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Merit::Score::Point Create (0.4ms)  INSERT INTO "merit_score_points" ("score_id", "num_points", "created_at") VALUES ($1, $2, $3) RETURNING "id"  [["score_id", 33], ["num_points", 50], ["created_at", "2020-04-15 15:54:38.207458"]]
   (0.4ms)  COMMIT
   (0.2ms)  BEGIN
  Merit::Action Load (0.5ms)  SELECT "merit_actions".* FROM "merit_actions" WHERE "merit_actions"."id" = $1 LIMIT $2  [["id", 255], ["LIMIT", 1]]
  Merit::ActivityLog Create (0.4ms)  INSERT INTO "merit_activity_logs" ("action_id", "related_change_type", "related_change_id", "description", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["action_id", 255], ["related_change_type", "Merit::Score::Point"], ["related_change_id", 62], ["description", "a obtenu 50 points"], ["created_at", "2020-04-15 15:54:38.221476"]]
   (0.5ms)  COMMIT

Interestingly, points do get granted to users once they have created their account and confirmed their email address. Here is the trace:

  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 253], ["LIMIT", 1]]
  CACHE User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 253], ["LIMIT", 1]]
  Merit::Sash Load (0.8ms)  SELECT "sashes".* FROM "sashes" WHERE "sashes"."id" = $1 LIMIT $2
  [["id", 26], ["LIMIT", 1]]
  Merit::Score Load (1.3ms)  SELECT "merit_scores".* FROM "merit_scores" WHERE "merit_scores"."sash_id" = $1 AND "merit_scores"."category" = $2 ORDER BY "merit_scores"."id" ASC LIMIT $3  [["sash_id", 26], ["category", "default"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Merit::Score::Point Create (1.0ms)  INSERT INTO "merit_score_points" ("score_id", "num_points", "created_at") VALUES ($1, $2, $3) RETURNING "id"  [["score_id", 33], ["num_points", 10], ["created_at", "2020-04-15 15:48:40.136287"]]
   (0.3ms)  COMMIT
   (0.1ms)  BEGIN
  Merit::Action Load (0.2ms)  SELECT "merit_actions".* FROM "merit_actions" WHERE "merit_actions"."id" = $1 LIMIT $2  [["id", 251], ["LIMIT", 1]]
  Merit::ActivityLog Create (0.2ms)  INSERT INTO "merit_activity_logs" ("action_id", "related_change_type", "related_change_id", "description", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["action_id", 251], ["related_change_type", "Merit::Score::Point"], ["related_change_id", 59], ["description", "a obtenu 10 points"], ["created_at", "2020-04-15 15:48:40.149270"]]

Thank you in advance!

gabrielguerin commented 4 years ago

Interestingly, if I change to: :action_user to to: :user, it does work even though the trace is the same. I do not know why however.

The full working code in point_rules is therefore:

    def initialize
      # New user
      score 10,
            on: 'users/confirmations#show',

            model_name: 'User',

            to: :itself

      # If user adds a post
      score 50,
            to: :user,

            on: 'posts#create'

      # If user downloads a file
      score -50,
            to: :action_user,

            on: 'active_storage/blobs#show'
    end