【问题标题】:mongoid how to get all documents in a collectionmongoid 如何获取集合中的所有文档
【发布时间】:2015-12-09 10:00:23
【问题描述】:

我知道我的问题相当愚蠢,但我查看了 mongoid 教程和 SO 试图弄清楚如何使用 mongoid 获取集合中的所有文档,这是 mongo shell 中 db.collection.find() 的对应物.

许多 SO 问题都需要一些标准,我只想获取所有这些,而不是嵌入文档,只是 db.collection.find() 的相同结果,但来自 mongoid。

我需要这个,因为在我的邮件预览中,我想让所有用户看到发送给他们的电子邮件并确认他们的帐户,我在这里使用带有 Rails 4.2.5 和 mongoid 5 的设计。

我尝试了 Model.find 和 Model.find_by 和 Model.pluck,第一个返回 nil,第二个只返回一个文档(因为我为 得到了未定义的方法),最后一个会只返回一个键的值。

那么怎么做呢?

编辑

这是我的模型:

class Researcher 

include Mongoid::Document
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :validatable,:confirmable,:recoverable,:omniauthable, :omniauth_providers => [:gplus]
  #, :rememberable, :trackable

  ## Database authenticatable
  field :email,              type: String, default: ""
  field :encrypted_password, type: String, default: ""

  ## Recoverable
  field :reset_password_token,   type: String
  field :reset_password_sent_at, type: Time,localize: true

  ## Rememberable
  #field :remember_created_at, type: Time

  ## Trackable
  #field :sign_in_count,      type: Integer, default: 0
  #field :current_sign_in_at, type: Time
  #field :last_sign_in_at,    type: Time
  #field :current_sign_in_ip, type: String
  #field :last_sign_in_ip,    type: String

  ## Confirmable
  field :confirmation_token,   type: String
  field :confirmed_at,         type: Time,localize: true
  field :confirmation_sent_at, type: Time,localize: true
  field :unconfirmed_email,    type: String # Only if using reconfirmable

  ## Lockable
  # field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
  # field :unlock_token,    type: String # Only if unlock strategy is :email or :both
  # field :locked_at,       type: Time
  field :username, type: String
  field :provider,type: String
  field :uid,type: String

  def self.new_with_session(params, session)
    super.tap do |researcher|
      if data = session["devise.gplus"] && session["devise.gplus"]["extra"]["raw_info"]
        researcher.email = data["email"] if researcher.email.blank?
      end
    end
  end

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |researcher|
    researcher.email = auth.info.email
    researcher.password = Devise.friendly_token[0,20]       
  end
end
end

这是我的邮件预览:

class ResearchersMailerPreview < ActionMailer::Preview 
  def initialize
    @vals=Researcher.all
  end
  def confirmation_instructions    
    @vals.each do |val|    
      ResearchersMailer.confirmation_instructions(val,val.confirmation_token , {})
    end
  end
  def reset_password_instructions
    @vals.each do |val|          
      ResearchersMailer.reset_password_instructions(val, val.reset_password_token, {})    
    end
  end
end

【问题讨论】:

  • @muistooshort 我更新了我的问题
  • Researcher.all 能为您做什么?这最终会抱怨find_first_mime_type吗?您确定问题出在Researcher.all 而不是您的ResearcherMailerPreview 方法之一的返回值吗?听起来您正在将 @vals 交给 ActionMailer 内部的某个东西,该东西期待某种邮件对象。
  • @muistooshort 是的,我得到了 find_first_mime_type,我不会将 @vals 传递给任何类型的 ActionMailer,它只是为了预览。
  • 但是您的 confirmation_instructionsreset_password_instructions 方法 return @vals@vals 将是 Mongoid::Contextual::Mongo 实例。 find_first_mime_type 异常应该有一个堆栈跟踪。
  • @muistooshort ahhh 我以为每个都返回 nil,现在我知道它返回调用它的数组。谢谢,我想我稍后会修复它并自己发布答案

标签: ruby-on-rails mongoid


【解决方案1】:

我认为你需要的是:

Model.all

然后您可以通过循环访问您的集合来获取它们中的每一个。

【讨论】:

  • 我得到“#<:contextual::mongo:0x007f2bccec0258> 的未定义方法 `find_first_mime_type'”
【解决方案2】:

Model.all

返回所有文档如果您的选择器为空。 为了在您已被预选时访问所有条目,请使用:

Model.unscoped.all

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 1970-01-01
    • 2023-03-28
    • 2021-06-29
    • 2020-08-12
    • 1970-01-01
    • 2022-01-18
    • 2020-08-09
    • 2011-10-23
    相关资源
    最近更新 更多