Error while using gem Merit on Ruby on Rails

150 views Asked by At

I'm making achievement system using gem merit 3.0.1 on Ruby on Rails version 5.1.4. But the problem comes when I'm trying to create User either on POST method or on rails console. The problem goes ActiveRecord::RecordInvalid: Validation failed: Sash must exist. I have followed this link https://github.com/merit-gem/merit/issues/265 and it says that I have to make the sash optional then pulling the custom merit gem by another github user, and it goes well, until when I see that sash_id of each user is nil, if sash_id is nil, it means the badges cannot make relationship with the user. What is the problem? I have tried every step that inside the github and stil no clue. Do I have manually create the sash_id each time User is registered? Thank you.

This is my user model

class User < ApplicationRecord
    include Merit
    has_merit

    has_many :activities, dependent: :destroy
    has_many :locations, as: :locationable

    has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
    has_many :following, through: :active_relationships, source: :followed

    has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy
    has_many :followers, through: :passive_relationships, source: :follower

    def follow(other_user)
        following << other_user
    end

    def unfollow(other_user)
        following.delete(other_user)
    end

    def following?(other_user)
        following.include?(other_user)
    end

    class << self

        def find_for_verfied_token_response(auth,provider,access_token)
            user = find_or_create_by(uid: auth['id'])

            if provider == "facebook"
                user.profile_picture = auth['picture']['data']['url']
            elsif provider == "google_oauth2"
                user.profile_picture = auth['picture']
            end

            user.email = auth['email']
            user.full_name = auth['name']
            user.gender = auth['gender']
            user.provider = provider
            user.access_token = access_token

            puts(user)
            user.save!
            user
        end

        def save_api_key(email,uid,api_key)
            user = User.where(email: email, id: uid).first
            user.api_key = api_key
            user.save!
            user
        end
    end
end
2

There are 2 answers

3
Ayham Orfali On

I just ran into the same problem. please use this branch

    gem 'merit', github: 'tamycova/merit', branch: 'f657596409d98fc7c8e3274b213b86cc47d87495'

and i think it'll fix it. BTW what version of rails are you using?

0
Javier Ortiz On

I did make a small fix, but need a lot of rework the fix. For now its working, the problem is in 'belongs_to' relationship. Merit is not full compatible with Rails 5.

# Insert into model app/models/user.rb
before_validation :set_sash

def set_sash
  sash = ActiveRecord::Base.connection.execute(query_sash)
  self.sash_id = sash.first["id"]
end

def query_sash
  'INSERT INTO sashes (created_at, updated_at) VALUES (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
   select id from sashes order by created_at desc limit 1;'
end

Don't hate me for the code, i solved that fast without thinking about that. Probably we have better options than this, if you know one of them, comment please.

Thanks.