How get deleted (deleted_at) fields with mongoid? - ruby

I can get some data with where() method, but if some records were deleted with Paranoia delete() method (the deleted_at field is set with the date of deletion) they are not returned in the results.
I can get those records using collection.deleted.entries.find() with Moped, but I need it as usual Mongoid criteria data.

The paranoia plugin sets a default_scope on the model.
included do
field :deleted_at, type: Time
class_attribute :paranoid
self.paranoid = true
default_scope where(deleted_at: nil)
scope :deleted, ne(deleted_at: nil)
define_model_callbacks :restore
end
You can tell Mongoid not to apply the default scope by using unscoped, which can be inline or take a block.
Band.unscoped.where(name: "Depeche Mode")
Band.unscoped do
Band.where(name: "Depeche Mode")
end

Related

Removing attributes from an activerecord got via .includes

I am having a really weird problem while attempting to do a very simple thing. I am doing an .includes on a model to get a row of data from the database. On the return object I need to remove certain attributes conditionally. And the final aim is to reinsert this row as a new record based on the changes I make on the attributes using my conditions.
def myUpdate
dbObj = Obj.includes(:name,
:addr1,
:addr2,
:state,
:description).find(params[:id])
#dbObjective.attributes().except('description')
#dbObjective.description = nil
#dbObjective.attributes().delete('description')
# After setting more attributes, persist this object
end
I tried all possibilities that I could think of, but the attribute is just not getting removed. What am I missing? I am on Ruby on Rails 4.2.
includes is used to include associated tables in your query for join queries and eager loading, not for table attributes. You do not need to do anything special to access an object's attributes.
attributes returns a Hash instance containing the record's attributes as key-value pairs, and operating on it will change only the Hash instance itself, not the record.
There are several ways to update attributes. One of the easiest ways is using the built in setter methods given to you by ActiveRecord. If you really want to change attributes using the Hash API you can store the attributes hash in a variable, manipulate the hash, and pass it as an argument to update, which accepts an attributes hash as it's argument.
Using setter methods
def myUpdate
dbObj = Obj.find(params[:id])
dbObj.description = 'new_description'
dbObj.name = 'new_name
dbObj.save
end
Using update
def myUpdate
dbObj = Obj.find(params[:id])
attributes = dbObj.attributes # This is how you would update the object by manipulating the attributes hash
attributes.delete(:description) # this will NOT end up changing the attribute in the DB
attributes[:name] = nil # this will successfully set name to NULL in the DB
dbObj.update(attributes) # pass the manipulated hash to the `update` method to persist the changes
end
deleteing fields from the hash will not have an effect on the persisted object. update only performs an insert on fields present in the hash that have changed.

How to get new mongoid indexes for a model

Let's say I have defined my model Person with a couple of indexes:
class Person
include Mongoid::Document
field :email
field :ssn
index({ email: 1 }, { unique: true })
index({ ssn: 1 }, { unique: true })
end
However, only the email index already exists in the database, so when I call
Person.collection.indexes.each {|i| puts i.inspect}
I get the following response:
{"v"=>1, "key"=>{"_id"=>1}, "name"=>"_id_", "ns"=>"x.person"}
{"v"=>1, "unique"=>true, "key"=>{"email"=>1}, "name"=>"email_1", "ns"=>"x.person"}
The question is, how can I get the list of defined indexes in the model, even if they are not already created in mongo ?
In my case, such list should include the definition for the field "ssn"
In other words...How to get those indexes that haven't been created yet ?
Person.index_specifications
shows the indexes defined in the model regardless of its existence in the database.
And
Person.collection.indexes
only shows the index that actually exists in the database.
So there is something else that is worth paying attention to:
rake db:mongoid:create_indexes
will create the indexes defined in the model in the database, and it uses the method 'index_specifications' in deed.
While this removes all the indexes other than the index of the primary key:
rake db:mongoid:remove_indexes
So when you want to only remove the indexes that exists in the database but no longer defined in the database, you should use this:
rake db:mongoid:remove_undefined_indexes
which use the method 'undefined_indexes' in deed.
I hope this can be helpful.
The docs are here:
https://mongoid.github.io/en/mongoid/docs/indexing.html
http://www.rubydoc.info/github/mongoid/mongoid/Mongoid/Tasks/Database#create_indexes-instance_method
Just found it...
We can get the list of all index definitions into the model as follows:
Person.index_specifications
This is an array populated when the application is loaded and is used by the "create_indexes" method as can be seen here:
https://github.com/mongodb/mongoid/blob/master/lib/mongoid/indexable.rb

Mongoid "find" returns nil, when "find_by" retrieves record

I'm using Mongoid to access a MongoDB database, however I'm running into an odd problem. It seems like I can only query for records using find_by, as find will always return nil:
invoices = Invoice.find({})
p "invoices"
p invoices
puts ''
invoice = Invoice.find_by({ _id: <ObjectId> })
p "invoice"
p invoice
puts ''
The second query using find_by will return a single record. According to the documentation, find should be returning every record that satisfies the query.
Does anyone have an idea what could be causing this?
Be careful not to confuse the Moped syntax with the Mongoid syntax. For Mongoid, the docs describe the find method:
Find a document or multiple documents by their ids. Will raise an error by default if any of the ids do not match
If you really want every record, Invoice.all can do the trick. (Also be careful with your find_by method. The Mongoid syntax varies from mongo's a bit, so you don't have to have the curlies around your params.)

Mongoid's .includes() Not Populating Relations

I am using Mongoid v4.0.2, and I'm running into an interesting issue using .includes(). I have a record that represents invoices, who has a list of charges.
I want to query for a single invoice and have the charges be populated after I run the query. According to the docs (search for "Eager Loading"), I should be able to do something like this to have Mongoid populate the charges:
Invoice.includes(:charges).find_by({ _id: <objectId> })
When I get the record back the charges are still showing up as a list of ObjectId's, and removing the .includes() seems to have no effect one way or another. I've verified each charge exists in the record I'm querying for, so I'm confused why they aren't populating.
I believe I have the data models set up correctly, but I'll include them here for completeness.
class Invoice
include Mongoid::Document
has_many :charges
field :status, type: String
field :created, type: Time, default: -> { Time.now }
end
class Charge
include Mongoid::Document
field :created, type: Time, default: -> { Time.now }
field :transactionId, type: String
field :category, type: String
field :amount, type: Float
field :notes, type: String
belongs_to :invoices
end
There is no reason to use includes if you are only finding one document. Just find the document and then access the relation. Either way, 2 database requests will be issued.
The only time includes provides a performance increase is when you are loading multiple relations for multiple documents, because what Mongoid will do is load the queried documents, go through and gather all of the ids that should be queried for all of those documents and then query for all relations as one database call using the :id.in => ids feature. In your case, there is no point to do this.

Multiple queries on single object in DataMapper

When I try to run the following code, DataMapper calls for 3 queries in just these two lines. Can anyone explain why it would do this?
#u = User.first(:uid => 1, :fields => [:uid, :name])
json #u
This calls the following queries:
SELECT "uid", "name" FROM "users" WHERE "uid" = 1 ORDER BY "uid" LIMIT 1
SELECT "uid", "email" FROM "users" WHERE "uid" = 1 ORDER BY "uid"
SELECT "uid", "accesstoken" FROM "users" WHERE "uid" = 1 ORDER BY "uid"
It is worth noting that datamapper has a validation on name for being => unique
Also, the accesstoken is lazily loaded so it should only be queried when asked for specifically, which must be happening when serializing it to a json object.
EDIT:
I have added my model class for clarification. I just want one query made for the uid and name without having to extract them individually from the object. Maybe this is the only way?
property :uid, Serial
property :name, String
property :email, String
property :accesstoken, Text
ANSWER:
Use the dm-serializer gem that has this support built-in
https://github.com/datamapper/dm-serializer
The first query is invoked by your User.first... call. Notice the fields it's selecting are what you requested - uid and name
The second and third queries are getting run in the json serialization, as it's lazy loading each property you didn't already load.
So you either need to do a custom serialization to only output uid and name for your users, or you should just remove the field selection from your initial query so it all gets loaded at once.
Update:
To do a custom serialization with datamapper, you can use the dm-serializer gem https://github.com/datamapper/dm-serializer and call #u.to_json(only: [:uid, :name])
Alternatively in this simple case you could just build the serialized object you want yourself, for which there are many examples: Rails3: Take controll over generated JSON (to_json with datamapper ORM)

Resources