RAILS Mongoid finding document by id - mongodb

When I do in rails console:
User.first
=> #<User _id: 6241f97de64d1eb2cc003d08,....
I get document, but when I try to find it by id like
User.find('6241f97de64d1eb2cc003d08') or User.find(BSON::ObjectId('6241f97de64d1eb2cc003d08')) or
User.find('6241f97de64d1eb2cc003d08'.to_bson) etc
I get:
message:
Document(s) not found for class User with id(s) 6241f97de64d1eb2cc003d08.
also
3.0.3 :049 > User.where(_id: BSON::ObjectId('6241f97de64d1eb2cc003d08')).find
=> nil
Why it does not work????
I use rails 7 and mongoid

Finally found the solution.
User model was defined like this:
class User
include Mongoid::Document
include Mongoid::Timestamps
field :_id, type: String
field :first_name, type: String
field :last_name, type: String
end
And changing it to this:
class User
include Mongoid::Document
include Mongoid::Timestamps
# field :_id, type: String
field :first_name, type: String
field :last_name, type: String
end
Commenting "field: _id" did the trick. I think string type was "covering" expected ObjectId as _id and find methods are converting strings into ObjectId so we ended up in search against string vs ObjectId.

Related

Mongoid 6, Rails 5, HABTM "unpermitted parmeter"

I've been doing RoR for years, but this is my first project with Mongo (also my first api-only project). I'm having a rough time with HABTM associations and I suspect it has to do with params, but I'm not sure what else to try.
Here's what I've got:
class Project
include Mongoid::Document
field :name, type: String
field :start_date, type: Date
field :target_date, type: Date
has_and_belongs_to_many :users
end
class User
include Mongoid::Document
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:jwt_authenticatable, jwt_revocation_strategy: JWTBlacklist
field :email, type: String
field :_id, type: String, default: ->{ email }
{ ... devise stuff ...}
has_and_belongs_to_many :projects
end
In my projects controller, I have this for parameters:
def project_params
params.permit(:id, :name, :start_date, :target_date, :description, user_ids: [])
end
And yes, I've also tried doing {user_ids: []}.
When I use Postman to make a URL PUT request to attempt to add users to a project, I get an "unpermitted parameter" error. But ... I'm permitting that parameter, right?
I'm kind of going nuts because I don't know if I have a Mongo problem, a Rails 5 problem, or an API problem. All other calls are working fine.
Started PUT "/rapi/projects/3" for 127.0.0.1 at 2017-02-15 22:55:10 -0500
Overwriting existing field _id in class JWTBlacklist.
Overwriting existing field _id in class User.
Processing by Api::V1::ProjectsController#update as JSON
Parameters: {"user_ids"=>"test#gmail.com", "id"=>"3"}
MONGODB | localhost:27017 | anthem.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>"test#gmail.com"}}
MONGODB | localhost:27017 | anthem.find | SUCCEEDED | 0.000363703s
Overwriting existing field _id in class Project.
MONGODB | localhost:27017 | anthem.find | STARTED | {"find"=>"projects", "filter"=>{"_id"=>"3"}}
MONGODB | localhost:27017 | anthem.find | SUCCEEDED | 0.000244022s
Unpermitted parameters: user_ids, format
Overwriting existing field _id in class Release.
Completed 204 No Content in 20ms
I'd appreciate any ideas about what else I might try.
But ... I'm permitting that parameter, right?
Not quite. You're permitting user_ids as an array. But you send it as a scalar value. This is enough difference for strong params to not let the data through.
Make up your mind and do either one (permit array and send array) or the other (permit scalar and send scalar).
I think you should use devise_parameter_sanitizer.permit
devise_parameter_sanitizer.permit(:id, :name, :start_date, :target_date, :description, keys: [:username])

Default datetime with Ecto & Elixir

I've just started working Elixir & Phoenix today, i am trying to add Ecto as a mapper, but i'm having some trouble using time.
This is my model.
schema "users" do
field :name, :string
field :email, :string
field :created_at, :datetime, default: Ecto.DateTime.local
field :updated_at, :datetime, default: Ecto.DateTime.local
end
I'm trying to set the created_at and updated_at per default, but when i try to compile this, i get the following error.
== Compilation error on file web/models/user.ex ==
** (ArgumentError) invalid default argument `%Ecto.DateTime{day: 13, hour: 19, min: 47, month: 2, sec: 12, year: 2015}` for `:datetime`
lib/ecto/schema.ex:687: Ecto.Schema.check_default!/2
lib/ecto/schema.ex:522: Ecto.Schema.__field__/4
web/models/board.ex:9: (module)
(stdlib) erl_eval.erl:657: :erl_eval.do_apply/6
There is not much help to get in the documentation, what would be the correct way to do this?
Defaults fields names are :inserted_at and :updated_at but you can merge with your own field names, passing a keyword list
schema "users" do
field :name, :string
field :email, :string
timestamps([{:inserted_at,:created_at}])
end
:datetime is the native Postgres data type for, well a datetime; this data type maps to a two-elements Elixir tuple ({{yy, mm, dd}, {hh, mm, ss}}). An %Ecto.DateTime{} struct is not a two-elements tuple, hence the compilation error.
You may want to set the type of your fields to Ecto.DateTime, it should all work seamlessly.
Here is the relevant documentation about primitive types and non-primitive types.
PS you may also want to have a look at Ecto.Schema.timestamps/1, which is macro that expands to basically what you wrote manually (it adds the created_at and updated_at fields and it let's you choose what type they should be, defaulting to Ecto.DateTime):
schema "users" do
field :name, :string
field :email, :string
timestamps
end
You could also consider having the default not be in the schema, but in the migration: "created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"

Mongoid/MongoDB: Order a query by the value of an embedded document?

I am attempting to order the results of a query by the value of a specific embedded document, but even with what seems to be a valid set of options and using the $elemMatch operator, my results are coming back in natural order.
My model is composed of Cards, which embeds_many :card_attributes, which in turn reference a specific CardAttributeField and contain an Integer value. I would like to be able to order a collection of Cards by that value.
I am able to isolate a collection of Cards which have a CardAttribute referencing a specific CardAttributeField like this:
cards = Card.where(:card_attributes.elem_match => {
:card_attribute_field_id => card_attribute_field.id
})
If I knew the order in which the card_attributes were set, I could use MongoDB array notation, like this:
cards.order_by(['card_attributes.0.value', :asc])
This does deliver my expected results in test scenarios, but it won't work in the real world.
After much messing around, I found a syntax which I thought would allow me to match a field without using array notation:
cards.asc(:'card_attributes.value'.elem_match => {
:card_attribute_field_id => card_attribute_field.id
})
This produced a set of options on the resulting Mongoid::Criteria which looked like:
{:sort=>{"{#<Origin::Key:0x2b897548 #expanded=nil, #operator=\"$elemMatch\", #name=:\"card_attributes.value\", #strategy=:__override__, #block=nil>=>{:card_attribute_field_id=>\"54c6c6fe2617f55611000068\"}}"=>1}}
However, the results here came back in the same order regardless or whether I called asc() or desc().
Is there any way to do what I'm after? Am I taking the wrong approach, or do I have a mistake in my implementation? Thanks.
Simplified, my model is:
class Card
include Mongoid::Document
# various other fields
has_many :card_attribute_fields
embeds_many :card_attributes do
def for_attribute_field card_attribute_field
where(:card_attribute_field_id => card_attribute_field.id)
end
end
end
class CardAttributeField
include Mongoid::Document
belongs_to :card
field :name, type: String
field :default_value, type: String
field :description, type: String
end
class CardAttribute
include Mongoid::Document
embedded_in :card
field :card_attribute_field_id, type: Moped::BSON::ObjectId
field :value, type: Integer
end

Get all keys in MongoMapper Model including its association

Suppose the following model :
class Product
include MongoMapper::Document
key :name, String
key :product_category_id, ObjectId
belongs_to :product_category
end
class ProductCategory
include MongoMapper::Document
key :name, String, :required => true, :unique => true
timestamps!
userstamps!
end
I want to implement an advanced search that will inspect all value inside my Model including its all association like :
I have :
Product A data named "aLaptop" belongs_to:ProductCategory named "Notebook".
Product B data named "aGreatNotebook" belongs_to:ProductCategory named "Notebook".
When I search with keyword named "Notebook", I want to search it to Product.name fields and also its associations which mean ProductCategory.name also. So it will returned both of that items, because Product A have ProductCategory.name "Notebook" & Product B have Product.name "aGreatNotebook" and ProductCategory "Notebook"..
How can I do this?? I've already searching for 2 days and not success until now:(.. When is in MySQL, I used join table.. But hows in MongoMapper??
Please help.. Thank you..
You can't do joins in MongoDB. So the basic idea is to get the ObjectId associated with the "Notebook" category and then to query the products where product_category is equal to notebook_id. This generally involves two queries. So that'd be something like this:
notebook_id = ProductCategory.first(:name => "Notebook")
if notebook_id
Product.where({:product_category_id => notebook_id['_id']})
end
The question is confusing, but the title of the question is clear.
So in case someone comes here hoping to see how to get all of the:
keys
associations
Read on...
To get the keys in a model:
ConfinedSpace.keys.keys
=> ["_id", "photo_ids", "include_in_qap", "position", "created_at", "updated_at",
"structure_id", "identifier", "name", "description", "notes", "entry_info",
"anchor_points", "nature", "special_equipment", "rescue_overview"]
And to get the associations:
ConfinedSpace.associations.each{|name,assoc| puts name}
photos
attachments
activities
structure
videos
And the class (edited for brevity):
class ConfinedSpace
include MongoMapper::EmbeddedDocument
include Shared::HasPhotos
include Shared::HasAttachments
include HasActivities
TAG = "ConfinedSpace"
belongs_to :structure
many :videos, :as => :attachable
key :identifier, String
key :name, String
key :description, String
key :notes, String
key :entry_info, String
key :anchor_points, String
key :nature, String
key :special_equipment, String
key :rescue_overview, String
validates :identifier, presence: true
end

has_one vs. defining as a Key for Embedded documents for MongoMapper and MongoDB

The Source code is
class RealTimeDetail
include MongoMapper::EmbeddedDocument
key :url, String
key :method, String
end
class TargetFeed
include MongoMapper::Document
key :name, String, :null => false
key :feed_type, String, :null => false
has_one :real_time_detail
end
When I do target_feed.real_time_detail = RealTimeDetail.new(:url => "http://example.com", :method => "get")
I get errored out.
Instead i've changed the TargetFeed to
class TargetFeed
include MongoMapper::Document
key :name, String, :null => false
key :feed_type, String, :null => false
key :real_time_detail, RealTimeDetail
end
This works but was wondering if this is the best way to go about it.
Your classes no longer inherit from ActiveRecord, and has_one belongs to ActiveRecord, so it's not available for use. Using an explicit key seems like it would work.
MongoMapper uses different syntax for implementing associations between data models. Details here: http://mongomapper.com/documentation/plugins/associations.html
the short of it is, instead of has_one, use one
class TargetFeed
include MongoMapper::Document
key :name, String, :null => false
key :feed_type, String, :null => false
one :real_time_detail
end
I believe that since you've defined RealTimeDetail as an embedded document you don't need to declare an association in the definition of RealTimeDetail. If RealTimeDetail included Document (instead of EmbeddedDocument) you would use belongs_to from it's end:
class RealTimeDetail
include MongoMapper::Document
key :url, String
key :method, String
belongs_to :target_feed
end