0

To query my MongoDB database I call:

MyClass.collection.find({ foo: "bar" })

If I want to constantize MyClass from a string I call:

"MyClass".safe_constantize.collection.find({ foo: "bar" })

This will return an array that I can work with. The array contains hash-like objects of the class BSON::Document.

But what if I want to query for a class that exists in the db, but not in my Rails app?

If I call "UnknownClass".safe_constantize it returns nil.

If I could move past that point I would have an array of regular key/value pairs to work with, and it wouldn't be a problem that the original class that was stored inside the db was unknown to my Rails app.

So how do I query for a class that my Rails app don't know about?

JohnSmith1976
  • 536
  • 2
  • 12
  • 35
  • Are you trying to say that table exist but you did not have model for that table right? – Ajay Barot Jan 28 '18 at 13:23
  • are you using mongoid or something else? – Anthony Jan 28 '18 at 14:52
  • 1
    Why constantize at all? Why not just build a query and submit as SQL? – moveson Jan 28 '18 at 14:54
  • @AjayBarot correct the table exists in the db. It just doesn't exist in my Rail app as a model. There is a good reason for that, but it seems unnecessary to go into that. – JohnSmith1976 Jan 28 '18 at 16:00
  • @Anthony I'm using the [Mongo driver](http://api.mongodb.com/ruby/current/Mongo/Collection.html) to query. Mongoid doesn't fit my use case for a few reasons. – JohnSmith1976 Jan 28 '18 at 16:03
  • @AjayBarot I would need an example, but `"MyClass".safe_constantize.collection.find({ foo: "bar" })` fits my use case in many ways, except for this constantization problem, so my question is quite specifically about how to make this `collection.find` query work. – JohnSmith1976 Jan 28 '18 at 16:06
  • @JohnSmith1976: Please check the answer, probably it will work. – Ajay Barot Jan 28 '18 at 16:50

1 Answers1

0

You can try this. Please let me know If its not working

sql = "SELECT * from unknown_table"
result = ActiveRecord::Base.connection.execute(sql)
result.collection.find({ foo: "bar" })

Edit:

This is not working because you are using mongoid thus you don't have rails ActiveRecord. You can try this if you are using Mongoid 3 or above.

db = Mongoid::Sessions.default

# inserting a new document
collection = db[:collection_name]
collection.find({ foo: "bar" }

Ref: How to query MongoDB directly from Ruby instead of using Mongoid?

Ajay Barot
  • 1,681
  • 1
  • 21
  • 37
  • when running line 2 I get `NameError: uninitialized constant ActiveRecord`. – JohnSmith1976 Jan 28 '18 at 17:23
  • thanks for the updated answer. For some reason I get `NameError: uninitialized constant Mongoid::Sessions`. There is a comment in the SO post you refer to about this, but I can't make any sense of it. – JohnSmith1976 Jan 28 '18 at 19:16
  • @JohnSmith1976: Are you testing this on rails console? – Ajay Barot Jan 28 '18 at 19:19
  • Please do this if you are testing this on rails console `require 'mongoid'` and `Mongoid.load!("path/to/your/mongoid.yml")` after that do as above mention. – Ajay Barot Jan 28 '18 at 19:22
  • Hm strange, it doesn't seem to help. Aside from the console I also ran the line of code in the app at runtime. – JohnSmith1976 Jan 28 '18 at 19:42
  • Hm strange, it doesn't seem to help. Aside from the console I also ran the line of code in the app at runtime. – JohnSmith1976 Jan 28 '18 at 19:42
  • @JohnSmith1976: Please attach gemfile which shows mongoid gems in question. – Ajay Barot Jan 28 '18 at 19:55
  • ` mongo (2.2.5) bson (~> 4.0) mongoid (5.1.3) activemodel (~> 4.0) mongo (~> 2.1) origin (~> 2.2) tzinfo (>= 0.3.37)` – JohnSmith1976 Jan 28 '18 at 21:00
  • 1
    It seems the connection gets establish differently depending on what mongoid version you run, but the overall answer seems to be correct. For me it worked with `db = Mongoid::Clients.default`. – JohnSmith1976 May 30 '18 at 09:23