0

我在 Ruby on Rails 5.1.4 版上使用 gem Merit 3.0.1 制作成就系统。但是当我尝试在 POST 方法或 Rails 控制台上创建用户时,问题就来了。问题来了ActiveRecord::RecordInvalid: Validation failed: Sash must exist。我已经关注了这个链接https://github.com/merit-gem/merit/issues/265并且它说我必须使窗扇成为可选然后由另一个 github 用户拉出自定义的优点宝石,它进展顺利,直到当我看到每个用户的nilsash_id 是 ,如果 sash_id 是nil,则意味着徽章无法与用户建立关系。问题是什么?我已经尝试了 github 中的每一步,但仍然没有任何线索。每次注册用户时我是否都手动创建了 sash_id?谢谢你。

这是我的用户模型

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
4

2 回答 2

0

我刚刚遇到了同样的问题。请使用这个分支

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

我认为它会解决它。顺便说一句,您使用的是什么版本的导轨?

于 2018-02-09T17:08:44.377 回答
0

我确实做了一个小修复,但需要大量返工修复。现在它的工作,问题在于'belongs_to'关系。Merit 与 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

不要因为代码而讨厌我,我没有考虑这一点就很快解决了。可能我们有比这更好的选择,如果您知道其中之一,请发表评论。

谢谢。

于 2018-04-13T08:55:49.007 回答