ruby-on-railsdevisemerit-gem

Merit does cease to work after create a second User with Devise


I have a tricky problem with Merit & Devise. When a member creates an account (the first member), he get's a badge, as it is in the rule below. Works perfect for him.

grant_on 'members/registrations#create', badge: 'WelcomeIn', model_name: 'Member'

If I log_out and create another Member for tests, the second member is created, but I have a Rollback in the server log (see below). And then no other Merit rule will work for the second member but Merit steel works for the first...

I really don't understand, the afternoon was spent for no solution...

In a short explaination, when it's working i have in the log :

SQL (0.5ms)  UPDATE "members" SET "sash_id" = $1, "updated_at" = $2 WHERE "members"."id" = $3  [["sash_id", 1], ["updated_at", "2018-05-19 17:15:30.716358"], ["id", 3]]

When its don't work, I get a rollback instead of the line above :

   (0.1ms)  ROLLBACK

I log_out and create another Member for tests. The member is created, but I have a Rollback in the server log (see below). And then Merit cease to work for him (the second Member) but Merit works for the first...

I really don't understand, the afternoon was spent for no solution...

registrations_controller

class Members::RegistrationsController < Devise::RegistrationsController

  # POST /resource
  def create
    @user = build_resource # Needed for Merit
    super

    if @member.persisted?
      MemberMailer.send_welcome(@member).deliver_now
      Notification.create!(category: "welcome", member: @member, topic: "Bienvenue sur MonApp !", checked: false)
    end

  end

EDIT & SOLUTION

If you have a Devise Model with another name than 'User' (like Member for me), you have to add a line in you Controller after the Merit doc setup (see below).

registrations_controller

class Members::RegistrationsController < Devise::RegistrationsController

  # POST /resource
  def create
    @user = build_resource # Needed for Merit
    @member = @user
    super
  end

Thanks @TuteC for the support.


Solution

  • After the @user = build_resource line in your controller, add @member = @user. Devise needs @user while you have merit configured to look for @member.