mongoid嵌入式文档标准未被识别

时间:2013-07-31 13:23:57

标签: ruby-on-rails mongoid

这是模型


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的新手,并试图尽我所能来解决问题,但我很难过。

1 个答案:

答案 0 :(得分: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 

# 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