ruby on rails - mongoid embedded document criteria not recognized -
here's models
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
and here's code i'm using try these models
# it's 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. should add database 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 database' }" end
everything works until line
scammer.requestors.where(requestor_ip: request.remote_ip).exists?
this lines causes error
undefined method `requestors' scammer:class
i've gone through mongoid.org , various other posts here , on other boards, , cannot find way access embedded requestor element off of scammers. new ruby , trying due diligence necessary fix issues myself, i'm stumped.
found answer--there nothing wrong models, it's code block in controller. apparently once return object using
where
lose ability search or use other functions such where, find , find_by. i re-wrote code block such, , works
if scammer.where(email_used: search_term).exists? scammerrec = scammer.find_by(email_used: search_term)
# though found parent object, still need use .where function pull parent again , check requestor_ip in 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. should add database 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 database' }" end
Comments
Post a Comment