Ruby on rails 获取创建消息ruby on rails mongoid的用户
我正在尝试一个查询,以获取在特定sala中创建消息的用户,但我找不到方法。 我有查询来获取特定sala的消息,但无法获取创建消息的用户 Mensajes_控制器(我从sala获得信息的地方) 设置_sala(获取我想要的信息的房间) 门萨耶模型Ruby on rails 获取创建消息ruby on rails mongoid的用户,ruby-on-rails,mongoid,Ruby On Rails,Mongoid,我正在尝试一个查询,以获取在特定sala中创建消息的用户,但我找不到方法。 我有查询来获取特定sala的消息,但无法获取创建消息的用户 Mensajes_控制器(我从sala获得信息的地方) 设置_sala(获取我想要的信息的房间) 门萨耶模型 class Mensaje include Mongoid::Document include Mongoid::Timestamps::Created field :cuerpo, type: String vali
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]
源代码'https://rubygems.org'
git|u源(:github){| repo |“https://github.com/#{repo}.git}
ruby“2.5.5”
#捆绑边缘Rails:gem'Rails',github'Rails/Rails'
gem'rails',“~>5.2.3”
#使用Puma作为应用程序服务器
宝石“彪马”,“大于3.11”
#轻松构建JSON API。阅读更多:https://github.com/rails/jbuilder
gem'jbuilder',“~>2.5”
#使用Redis适配器在生产中运行操作电缆
gem'redis',“~>4.0”
#使用ActiveModel具有\u安全\u密码
#gem'bcrypt',“~>3.1.12”
gem'bcrypt',“~>3.1.7”
宝石“mongoid bcrypt ruby”
宝石“设计”
宝石“kaminari mongoid”
宝石“mongo”
#使用Capistrano进行部署
#gem“capistrano rails”,集团::开发
gem'mongoid',“~>7.0.5”
#通过缓存减少启动时间;config/boot.rb中需要
gem'bootsnap','>=1.1.0',require:false
#使用机架CORS处理跨源资源共享(CORS),使跨源AJAX成为可能
宝石“机架cors”
gem“jwt_课程”,“~>2.3”
gem“mongoid rspec”,github:“mongoid/mongoid rspec”,参考:“68c95b133be1a1482fe882e39afd33262147d1f4”
小组:开发,:测试
#在代码中的任意位置调用“byebug”以停止执行并获得调试器控制台
gem'byebug',平台:[:mri,:mingw,:x64_mingw]
宝石“撬轨”
gem'rspec rails'
gem“工厂机器人轨道”
宝石“应该是匹配者”
结束
小组:发展怎么办
gem'listen','>=3.0.5','<3.2'
#Spring通过让应用程序在后台运行来加速开发。阅读更多:https://github.com/rails/spring
宝石“春天”
gem‘SpringWatcherListen’,“~>2.0.0”
结束
#Windows不包括zoneinfo文件,因此捆绑tzinfo数据
gem'tzinfo data',平台:[:mingw,:mswin,:x64_mingw,:jruby]
您可以随时提出任何您可能需要的问题。谢谢 这个怎么样
@mensaje.user
安装kaminari mongoid,也许还有mongo gem。这是我尝试时抛出的错误:NoMethodError(未定义的方法“user”for#)在创建
@mensaje
作为@mensaje=@sala.mensajes.desc(“'u id').limit(20)之前,尝试@mensaje
@mensaje
?尝试Mensaje.last.user
NoMethodError(未定义的方法用户用于nil:NilClass)
?是否确定此模型至少有一个对象?检查plsMensaje.all
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
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]