ruby-on-rails - 获取在 rails mongoid 上创建消息 ruby 的用户
问题描述
我正在尝试查询以获取在特定 sala 中创建消息的用户,但我找不到方法。我有查询来获取特定sala 的消息,但我无法获取创建该消息的用户。
Mensajes_controller(我从萨拉那里得到消息)
def index
@mensaje = @sala.mensajes.desc('_id').limit(20)
render json: @mensaje
end
set_sala(获取我想要消息的房间)
def set_sala
if params[:sala_id]
@sala = Sala.find(params[:sala_id])
end
end
门萨杰模型
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
用户模型
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
更新 - 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]
随意询问您可能需要的任何内容。谢谢!
解决方案
那这个呢?
@mensaje.user
并安装 kaminari-mongoid 和可能的 mongo gem。
推荐阅读
- python - 收到此错误:ModuleNotFoundError: No module named 'asgiref.local'
- java - Spring Boot JPA向MySQL插入实体/对象时如何自动插入外键
- apache-kafka - 如何修改kafka连接器?
- java - 使用 RxJava 同步两个异步 API 调用
- c# - 如何在 C# 中合并两个具有主键约束的数据表
- python - AttributeError:“元组”对象没有属性“profilePic”
- python - Python:在不改变段落顺序的情况下反转文件每个段落中的单词?
- c++ - ifstream 没有这样的文件或目录 C++
- java - 线程提交任务,不等待春天完成
- pip - 无法在 Windows 上导入 librosa lib - “找不到 sndfile”