【问题标题】:How to reference one model from inside another model?如何从另一个模型中引用一个模型?
【发布时间】:2013-12-05 01:22:35
【问题描述】:

如果用户(电子邮件地址)不在我的系统中,我会尝试向用户发送活动邀请(如果他们已存在于我的系统中)和网站邀请。以下是我的尝试:

  def self.lookup_and_send_emails(emails, msg)
    addresses = split_addresses(emails)
    addresses.each do |email|
      if Users.where(email: :email)
         SwapMailer.babysitter_request.deliver
      else
        #send the devise/website invitation
      end
    end
  end

但这给了我的是:

 uninitialized constant Event::Users

如何从我的事件模型访问我的用户模型。我可以把它移到控制器上,但我试图让我的控制器保持“瘦”。

【问题讨论】:

标签: ruby-on-rails model ruby-on-rails-4


【解决方案1】:

当您使用模型类时,它是单数的。正确的语法是:

if User.where(email: :email)
  SwapMailer.babysitter_request.deliver
else
  #send the devise/website invitation
end

【讨论】:

  • 可能会检查长度或计数,它不会是 nil - 即 if User.where(email: :email).count == 0if User.where(email: :email).any?
猜你喜欢
  • 2011-05-13
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
相关资源
最近更新 更多