简体   繁体   English

获取创建消息的用户 ruby on rails mongoid

[英]Get user that created a message ruby on rails mongoid

I'm trying a query to get the user that created a message in a specific sala but I can't find the way.我正在尝试查询以获取在特定大厅中创建消息的用户,但我找不到方法。 I've got the query to get the messages of the specific sala, but I can't get the user that created the message.我有查询来获取特定 sala 的消息,但我无法获取创建该消息的用户。

Mensajes_controller (where I get the messages from the sala) Mensajes_controller(我从萨拉获得消息的地方)

def index
    @mensaje = @sala.mensajes.desc('_id').limit(20)
    render json: @mensaje
end

set_sala (to get the room from which I want the messages) set_sala(获取我想要消息的房间)

def set_sala
  if params[:sala_id]
    @sala = Sala.find(params[:sala_id])
  end
end

mensaje model男装模特

class Mensaje
  include Mongoid::Document
  include Mongoid::Timestamps::Created

      field :cuerpo, type: String

      validates :cuerpo, presence: true

      belongs_to :user, foreign_key: :user_id
      belongs_to :sala, foreign_key: :sala_id

end

user model用户模型

class User
    include Mongoid::Document
    include ActiveModel::SecurePassword

      has_secure_password
      field :password_digest, type: String
      field :email, type: String
      field :nombre, type: String

      has_many :salas
      has_many :mensajes

      validates :nombre, presence: true
      validates :email, presence: true
      validates :password_digest, presence: true

end

Updating - Gemfile更新 - Gemfile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.5'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.12'
gem 'bcrypt', '~> 3.1.7'
gem 'mongoid-bcrypt-ruby'

gem 'devise'

gem 'kaminari-mongoid'
gem 'mongo'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

gem 'mongoid', '~> 7.0.5'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'

gem 'jwt_sessions', '~> 2.3'
gem 'mongoid-rspec', github: 'mongoid/mongoid-rspec', ref: '68c95b133be1a1482fe882e39afd33262147d1f4'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'pry-rails'
  gem 'rspec-rails'
  gem 'factory_bot_rails'
  gem 'shoulda-matchers'
end

group :development do
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end


# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Feel free to ask anything you may need.随时询问您可能需要的任何东西。 Thanks!谢谢!

What about this?那这个呢?

@mensaje.user

and install kaminari-mongoid and maybe mongo gem.并安装 kaminari-mongoid 和 mongo gem。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM