0

这是模型


class Scammer
  include Mongoid::Document
  field :email_used
  field :phone_used
  field :name_used
  field :first_logged, type: DateTime
  field :last_scam_attempt, type: DateTime
  field :checked, type: Integer, default: 0
  field :scams_count, type: Integer
  field :common_commodity
  field :status
  embeds_many :reports
  embeds_many :reporters
  embeds_many :requestors
end

class Report
    include Mongoid::Document
  embedded_in :scammer
    field :reported, type: DateTime
    field :posed_as
    field :encountered_through
    field :commodity
    field :details
    field :logged_by
end 

class Reporter
  include Mongoid::Document
  embedded_in :scammer
  field :reporter_ip
  field :captured, type: DateTime
end 

class Requestor
  include Mongoid::Document
  embedded_in :scammer
  field :requestor_ip
  field :captured, type: DateTime
end 

现在这是我用来尝试对这些模型做某事的代码



    # It's an email address, 
    if Scammer.where(email_used: @search_term).exists?
        if not Scammer.requestors.where(requestor_ip: request.remote_ip).exists?
        Scammer.requestors.create(requestor_ip: request.remote_ip, captured: DateTime.current()).save
                        end 
                        @return = Scammer.where(email_used: @search_term).to_json
                    else 
                        # No entry found. We should now add this to the database as a search
                        @newscammer = Scammer.new(email_used: @search_term, checked: 1, first_logged: DateTime.current(), status: "Seems Legit")
                        @newscammer.requestors.create(requestor_ip: request.remote_ip, captured: DateTime.current())
                        @newscammer.save
                        @return = "{ 'message' : 'Email added to database' }"
                    end 

一切正常,直到这条线

Scammer.requestors.where(requestor_ip: request.remote_ip).exists?

此行导致此错误

undefined method `requestors' for Scammer:Class

我已经浏览了 Mongoid.org 以及此处和其他板上的各种其他帖子,但我找不到从 Scammers 访问嵌入式 Requestor 元素的方法。我是 Ruby 的新手,并试图自己解决问题所需的尽职调查,但我很难过。

4

1 回答 1

0

找到了答案——模型没有问题,是控制器中的代码块。显然,一旦您使用

where
您将无法搜索或使用其他功能,例如 where、find 和 find_by。

我这样重写了代码块,现在它可以工作了

if Scammer.where(email_used: search_term).exists? scammerRec = Scammer.find_by(email_used: search_term)

      # Even though we found the parent object, we still need to use the .where function to pull the parent again and check for the requestor_ip in the requestors embedded doc
      if not Scammer.where(email_used: search_term, "requestors.requestor_ip" => request.remote_ip).exists?
        scammerRec.requestors.create(:requestor_ip => request.remote_ip)
      end 
      returnjson = Scammer.where(email_used: search_term).to_json
    else 
      # No entry found. We should now add this to the database as a search
      newscammer = Scammer.new(email_used: search_term, checked: 1, first_logged: DateTime.current(), status: "Seems Legit")
      requestor = Requestor.new
      requestor.requestor_ip = request.remote_ip
      Scammer.collection.update(newscammer.selector, {'$push' => {requestors: requestor.as_document}}, multi: true)
      newscammer.save
      returnjson = "{ 'message' : 'Email added to database' }"
    end 

于 2013-07-31T16:50:49.787 回答