In order to find a Root Document that contains a embedded document using MongoID/Rails 3 I need to do my query this way:
QuoteRequest.where( "order_request_items._id" => BSON::ObjectID(params[:id]) ).first
Is there a way to query without using the BSON::ObjectID ?
Thanks!
I'm not a MongoID/Rails user, but my guess is that you can't.
Even in the Mongo shell you have to use ObjectId() if you want to compare ObjectIDs. Something like this won't return any results:
db.foo.find({_id: "4c7ca651db48000000002277"})
You'll have to create an actual ObjectID from the string in order to get results:
db.foo.find({_id: ObjectId("4c7ca651db48000000002277")})
MongoID apparently doesn't automatically convert your input to ObjectIDs. But perhaps there's a way to tell MongoID which fields it should always convert to ObjectIDs? Then you would be able to omit the use of BSON::ObjectID.
This is a bug, the ids should be automagically converted by Mongoid. You should open a ticket on github: http://github.com/mongoid/mongoid/issues
Related
I need to do some bulk updates in some models and set value of a field as value of another field.
Right now I can do that with raw sql like this:
ActiveRecord::Base.connection.execute("UPDATE `deleted_contents` SET `deleted_contents`.`original_id` = `deleted_contents`.`id` WHERE `deleted_contents`.`original_id` is NULL")
This is working fine, however I need to do this using ActiveRecord query interface due to many reasons.
I tried:
DeletedContent.where(original_id: nil).update_all(original_id: value_of_id_column)
For value_of_id_column I tried :id, self.id, id, etc, nothing works. What should I set for value_of_id_column to get the original query generated by rails? Is this possible, or using the raw sql is the only solution?
Also I do not want to iterate over each record and update. This is not a valid solution for me:
DeletedContent.where(original_id: nil).each do |deleted_content|
update_each_record
end
I'm pretty sure you cannot obtain that query by passing a hash to update_all.
The closest to what you want to obtain would be:
DeletedContent.where(original_id: nil).update_all("original_id = id")
I am using Mongoid 3.1.6 with Rails 4. I need to find all the objects 'near' a certain co-ordinate. For each result from the search, I will need to display the distance from the search co-orodinate. According to Mongoid Documentation
...each instantiated document from a $geoNear query will get a special
dynamic attribute geo_near_distance that will be available as long as
the document is in memory.
But I am not able to access the Object.geo_near_distance
My query inside controller...
#objects = Object.geo_near([-118.4451, 34.0633]).max_distance(10)
Edit#1
Some additional details
If the use the following query in MongoDB
db.runCommand( { geoNear: "objects",
near: [ -73.95269,40.77578],
spherical: true
})
I see an array of 100 elements. Each element has 2 attributes. The first one, 'dis' has values like '0.000123' (Note: this is not in Km or Mile) and the second attribute is the result Object itself.
Now I have changed the query to Mongoid to...
#objects = Object.geo_near([-118.4451, 34.0633]).spherical.max_distance(10)
still no result.
Thanks in advance for your help.
After more than 2 years, the issue ticket is still open on mongodb jira tracker.
The quick fix is not use the hash notation instead of the dot notation to access the attribute:
Instead of
Object.geo_near_distance
Use
Object['geo_near_distance']
Tested on mongoid 6
Are you accessing the field while you are iterating the documents? You can see by the specs that this field is in fact there when the document is in memory and is being part of the iteration of the criteria result.
https://github.com/mongoid/mongoid/blob/master/spec/mongoid/contextual/geo_near_spec.rb#L167
Is it possible to quickly retrieve the datatype of a given Mongoid field?
Something like: FieldName.type?
Thanks
I guess, this is not just mongo specific. You can use the following code to retrieve field type of a field irrespective of the database underneath.
User.first.name.class
=> String
User.first.up_votes.class
=> Fixnum
I am still on mongoid 2.4 so I am not sure if this will work on 3.0 too:
User.fields["field_name"].options[:type]
Seems like it should be able to look at a simple tutorial or find an aswer with a quick google, but I can't...
codes = PartnerCode.find_by_sql "SELECT * from partner_codes where product = 'SPANMEX' and isused = 'false' limit 1"
I want the column named code, I want just the value. Tried everything what that seems logical. Driving me nuts because everything I find shows an example without referencing the actual values returned
So what is the object returned? Array, hash, ActiveRecord? Thanks in advance.
For Rails 4+ (and a bit earlier I think), use pluck:
Partner.where(conditions).pluck :code
> ["code1", "code2", "code3"]
map is inefficient as it will select all columns first and also won't be able to optimise the query.
You need this one
Partner.where( conditions ).map(&:code)
is shorthand for
Partner.where( conditions ).map{|p| p.code}
PS
if you are often run into such case you will like this gem valium by ernie
it gives you pretty way to get values without instantiating activerecord object like
Partner.where( conditions ).value_of :code
UPDATED:
if you need access some attribute and after that update record
save instance first in some variable:
instance=Partner.where( conditions ).first
then you may access attributes like instance.code and update some attribute
instance.update_attribute || instance.update_attributes
check documentation at api.rubyonrails.org for details
I search google for this question but i did not find any solution for
that.
I want to save parameter value to database. this parameter send by url.
for example:
http://simple-beach-416.heroku.com/code/restserver?fname=cv
I want to save fname value(here "cv") to database.
how can i do that?
Make a model for it? This is very vague... but define a model called Fname and give it the attribute "value"
Then...
Fname.create(:value => params[:fname])
I think this is where you're going, yeah?