Почему моя модель выдает эту ошибку при сохранении? - PullRequest
0 голосов
/ 21 июня 2020

Я получаю сообщение об ошибке всякий раз, когда пытаюсь сохранить запись. Я понятия не имею, что вызывает эту ошибку. После поиска в Google выяснилось, что это как-то связано с Active Storage, но я действительно не использую его в этом приложении.

Моя модель:

class User < ApplicationRecord
    has_and_belongs_to_many :chat_rooms, :join_table => :chat_rooms_and_users
    has_secure_password

    before_create :generate_nickname

    def generate_nickname
        puts "here"
        self.nickname = "rando_" + rand(99..999).to_s
    end

    def join_room room_name = nil
        if !room_name
            chat_room = ChatRoom.default_room
        else
            chat_room = ChatRoom.find_by name: room_name
        end
        chat_room.users << self unless chat_room.users.include?(self)
    end

    def self.find_or_create_by_client_token client_token
        @user = self.find_by(client_token: client_token)
        if not @user
            @user = self.new
            @user.client_token = client_token
            @user.chat_rooms << ChatRoom.default_room
        end
        @user.online = true
        @user.save # this is the line that generates the error
        return @user
    end
end

Ошибка :

There was an exception - URI::GID::MissingModelIdError(Unable to create a Global ID for User without a model id.)
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/globalid-0.4.2/lib/global_id/uri/gid.rb:167:in `validate_model_id'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/globalid-0.4.2/lib/global_id/uri/gid.rb:151:in `set_model_components'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/globalid-0.4.2/lib/global_id/uri/gid.rb:136:in `check_path'
C:/Ruby26-x64/lib/ruby/2.6.0/uri/generic.rb:807:in `path='
C:/Ruby26-x64/lib/ruby/2.6.0/uri/generic.rb:193:in `initialize'
C:/Ruby26-x64/lib/ruby/2.6.0/uri/generic.rb:137:in `new'
C:/Ruby26-x64/lib/ruby/2.6.0/uri/generic.rb:137:in `build'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/globalid-0.4.2/lib/global_id/uri/gid.rb:92:in `build'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/globalid-0.4.2/lib/global_id/uri/gid.rb:68:in `create'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/globalid-0.4.2/lib/global_id/global_id.rb:15:in `create'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/globalid-0.4.2/lib/global_id/identification.rb:8:in `to_global_id'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/globalid-0.4.2/lib/global_id/identification.rb:13:in `to_gid_param'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/connection/identification.rb:39:in `block in connection_gid'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/connection/identification.rb:37:in `map'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/connection/identification.rb:37:in `connection_gid'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/connection/identification.rb:29:in `connection_identifier'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/connection/internal_channel.rb:15:in `subscribe_to_internal_channel'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/connection/base.rb:170:in `handle_open'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/server/worker.rb:60:in `block in invoke'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/server/worker.rb:41:in `block in work'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/callbacks.rb:109:in `block in run_callbacks'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/server/worker/active_record_connection_management.rb:16:in `block in with_database_connections'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/connection/tagged_logger_proxy.rb:24:in `block in tag'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/tagged_logging.rb:71:in `block in tagged'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/tagged_logging.rb:28:in `tagged'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/tagged_logging.rb:71:in `tagged'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/connection/tagged_logger_proxy.rb:24:in `tag'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/server/worker/active_record_connection_management.rb:16:in `with_database_connections'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/callbacks.rb:118:in `block in run_callbacks'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/engine.rb:62:in `block (4 levels) in <class:Engine>'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/execution_wrapper.rb:87:in `wrap'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/engine.rb:57:in `block (3 levels) in <class:Engine>'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/callbacks.rb:118:in `instance_exec'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/callbacks.rb:118:in `block in run_callbacks'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/callbacks.rb:136:in `run_callbacks'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/server/worker.rb:40:in `work'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/server/worker.rb:58:in `invoke'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actioncable-5.2.4.3/lib/action_cable/server/worker.rb:53:in `block in async_invoke'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:353:in `run_task'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:342:in `block (3 levels) in create_worker'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:325:in `loop'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:325:in `block (2 levels) in create_worker'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:324:in `catch'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:324:in `block in create_worker'

ApplicationRecord:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

ChatRoom:

class ChatRoom < ApplicationRecord
  has_and_belongs_to_many :users, :join_table => :chat_rooms_and_users
  has_many :messages
  validates_uniqueness_of :name

  DEFAULT_ROOM_NAME = "General"

  def self.default_room
    chat_room = ChatRoom.find_by name: self::DEFAULT_ROOM_NAME
  end
end

1 Ответ

0 голосов
/ 21 июня 2020

Я полагаю, что каким-то образом вы вызываете трансляцию вновь созданного кабеля данных к действию. Он берет экземпляр класса User и пытается преобразовать его в GID , но для успешного выполнения этого экземпляра необходимо сохранить экземпляр в БД, чтобы иметь идентификатор. В модели User есть has_secure_password , по умолчанию требуется установка пароля. В find_or_create_by_client_token вы создаете пользователя без пароля, это может привести к сбою проверки. Вы можете отладить его так:

def self.find_or_create_by_client_token client_token
  @user = self.find_by(client_token: client_token)
  if not @user
    @user = self.new
    @user.client_token = client_token
    @user.chat_rooms << ChatRoom.default_room
  end
  @user.online = true
  @user.valid?
  puts @user.errors
  return @user
end

Попробуйте изменить has_secure_password на has_secure_password validations: false или добавить код для заполнения полей password и password_confirmation.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...