spohlenz / mongomodel Goto Github PK
View Code? Open in Web Editor NEWRuby ORM for MongoDB (compatible with Rails 3)
License: MIT License
Ruby ORM for MongoDB (compatible with Rails 3)
License: MIT License
class User < MongoModel::Document
has_many :posts
end
class Post < MongoModel::Document
belongs_to :user
property :state, String
scope :published, where(:state => 'published')
end
Post.published
=> is working.
User.first.posts.published
=> throws an error: "undefined method 'published' for Array"
this was working till i did a bundle install
this morning.
How would I go about using mongo's $or functionality within mongomodel? I have two scopes that I need to merge as an OR.
scope :expires_in_future, -> { where({:expires_at.gte => Time.now}) }
scope :expires_null, -> { where({:expires_at => :nil }) }
thanks
Kyle Banker stated somewhere that ObjectIds take up only 12 byte (string ids take up 16 bytes) and that mongodb can handle ObjectIds much faster than String IDs.
http://www.slideshare.net/kbanker/mongodb-rules-mwrc-2010 (page 21/22; there is also a video somewhere)
do you want to change mongomodel::reference to use ObjectIds instead of strings?
maybe with some rake task to update existing records?
We are using MongoModel, Wheelhouse and are unable to get them working with Mongo 2.6. Are these compatible?
mongo (1.9.2) lib/mongo/collection.rb:1086:in `rescue in generate_indexes'
mongo (1.9.2) lib/mongo/collection.rb:1079:in `generate_indexes'
mongo (1.9.2) lib/mongo/collection.rb:552:in `create_index'
mongomodel (0.5.2) lib/mongomodel/support/instrumented_collection.rb:97:in `block in create_index'
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.17) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `instrument'
mongomodel (0.5.2) lib/mongomodel/support/instrumented_collection.rb:125:in `instrument'
mongomodel (0.5.2) lib/mongomodel/support/instrumented_collection.rb:96:in `create_index'
mongomodel (0.5.2) lib/mongomodel/document/indexes.rb:40:in `block in ensure_indexes!'
mongomodel (0.5.2) lib/mongomodel/document/indexes.rb:39:in `each'
mongomodel (0.5.2) lib/mongomodel/document/indexes.rb:39:in `ensure_indexes!'
mongomodel (0.5.2) lib/mongomodel/support/scope.rb:192:in `ensure_indexes!'
mongomodel (0.5.2) lib/mongomodel/support/scope.rb:159:in `_find'
mongomodel (0.5.2) lib/mongomodel/support/scope.rb:165:in `_find_and_instantiate'
mongomodel (0.5.2) lib/mongomodel/support/scope.rb:43:in `to_a'
mongomodel (0.5.2) lib/mongomodel/support/scope/finder_methods.rb:29:in `first'
mongomodel (0.5.2) lib/mongomodel/document/scopes.rb:14:in `first'
wheelhouse-core (1.0.18) app/models/wheelhouse/site.rb:37:in `for_request'
wheelhouse-core (1.0.18) lib/wheelhouse/middleware/set_site.rb:21:in `site_for'
wheelhouse-core (1.0.18) lib/wheelhouse/middleware/set_site.rb:12:in `call'
request_store (1.0.5) lib/request_store/middleware.rb:9:in `call'
lib/middleware/caching_with_request_forgery_protection.rb:8:in `call'
lib/middleware/default_headers.rb:8:in `call'
Hi,
I just tried mongomodel on Rails 3.0.0.beta3 and encountered the following error:
No such file to load -- active_support/core_ext/object/metaclass (LoadError)
The metaclass.rb exists until Rails 3.0.0.beta but is missing from beta3.
Thanks in advance.
What do you think of adding to Collection
alias_method :build, :<<
It just looks a little more readable to build up collections from value hashes with:
collection.build(:name => "Test", :value => 10)
instead of:
collection << {:name => "Test", :value => 10}
We are trying to do something like
class Application
belongs_to :job, class: Job, foreign_key: :source_id
end
So that when we do some_application.job
it returns jobs by source_id
and when setting some_application.job = job
we would what it to set the job id reference to job.source_id
.
Is this possible with mongomodel?
Are there any plans to interface with GridFS or is there anyway to do it currently?
I fixed some of the rails 3.1 deprecation warnings.
unfortunatly my commits are a little clustered, so here a quick overview of what to change:
Fix path warnings:
https://github.com/max-power/mongomodel/blob/master/lib/mongomodel/railtie.rb#L20
Fix class_attributes warnings:
https://github.com/max-power/mongomodel/blob/master/lib/mongomodel/support/map.rb#L15-16
https://github.com/max-power/mongomodel/blob/master/lib/mongomodel/support/collection.rb#L17
haven't tackled the other ones.
Adds config.mongo_model to application.rb
https://github.com/max-power/mongomodel/blob/master/lib/mongomodel/railtie.rb#L3
Adds ActiveSupport load hook to MongoModel::Document
https://github.com/max-power/mongomodel/blob/master/lib/mongomodel/document.rb#L31
I've coded up a reload method for the persistence methods similar to what ActiveRecord provides.
http://github.com/bdurand/mongomodel/commit/0aef90d39d1071ec9ca79c5bdede7ccff882a7cb
The delete instance method in persistence is public. Calling this would bypass :destroy callbacks and could lead to some data integrity issues. I would think delete should be private. Or am I missing something?
i found out about the :transformer option on the mongo ruby drivers find method. from the docs:
:transformer (Block) โ default: nil โ a block for tranforming returned documents. This is normally used by object mappers to convert each returned document to an instance of a class.
then i tried something like this in the console and it worked great.
transformer = lambda {|doc| doc.delete('_type').constantize.from_mongo(doc) }
collection.find({}, {:transformer => transformer })
but i couldn't get the _find or _find_and_instantiate method (mongomodel::scope) running without breaking tests.
maybe you can have a look at it?
i thought it is possible to use the following:
index :ll => ::Mongo::GEO2D
but it will create a normal asc index on ll.
it would also be nice to implement the "$near" operator in mongomodel:
http://www.mongodb.org/display/DOCS/Geospatial+Indexing
https://github.com/spohlenz/mongomodel/blob/master/lib/mongomodel.rb#L61
there is still the autoload call, although you removed the file in this commit 7d7c04d
lib/mongomodel/concerns/validations.rb L:19 does an errors.clear, instead of respecting any user added errors. If (in the model) I do an errors.add_to_base or errors.add, my added errors are disregarded when i call model.valid?
Hi,
I am going to like the MongoModel.
It is simpler and more straightforward than other mongo mappers.
Well, I have a question.
I wrote the following two classes:
class Group < MongoModel::Document
property :name, String
has_many :members
end
class Member < MongoModel::Document
property :name, String
belongs_to :group
end
Then I tested them:
g = Group.create(:name => 'X')
g.members << Member.new(:name => 'Alice')
g.members << Member.new(:name => 'Bob')
from_db = Group.find(g.id)
puts from_db.members.size
I expected '2' as the result by analogy with ActiveRecord.
But I got '0' instead.
Is this intentional?
Since the changes in MongoModel::Reference the queries for array or collection properties are broken:
it used to work with symbols, but now it only works with strings
see my example:
class Article < MongoModel::Document
property :tags, Array, :default => []
end
Article.create(:tags => ['a', 'b', 'c'])
Article.where(:tags => 'a').count # error
Article.where('tags' => 'a').count # works as expected
i tracked the error down to the MongoOptions::extract_condtions method:
https://github.com/spohlenz/mongomodel/blob/master/lib/mongomodel/support/mongo_options.rb#L38-43
Calling a Document initializer with nil breaks instead of treating the nil like an empty hash. This just causes a few bugs when converting ActiveRecord models into MongoModel::Documents. This could be fixed with a nil check in the initialize method in concerns/attributes.rb.
I'm having a problem where I have a document with a property that is a Collection of EmbeddedDocuments. I can save it fine the first time, but when I reload the record, add another embedded document, and save it again, I get a stack level too deep error on the save callbacks.
I haven't found the problem, but the following spec recreates it:
describe "callbacks on many embedded documents" do
define_class(:ChildThingDocument, EmbeddedDocument) do
include MongoModel::CallbackHelpers
property :name, String
end
define_class(:ParentDocument, Document) do
property :things, Collection[ChildThingDocument]
end
it "should call callbacks on all embedded documents when adding a new one" do
parent = ParentDocument.create!
parent.things = Collection[ChildThingDocument].new
parent.things << ChildThingDocument.new(:name => "Thing One")
parent.save!
parent = ParentDocument.find(parent.id)
parent.things << ChildThingDocument.new(:name => "Thing Two")
parent.save!
end
end
I have been using Wheelhouse CMS for a project for over the last year along with Compose.io (formerly MongoHQ) which recommends using replica sets. For a while I would receive a notice from compose.io that one of my replica sets would be stepping down in favor of the other and I would have to manually switch out the database.yml file. However, after some digging I realized that mongomodel
supports the use of replicas. I only found this, though, after deeply reviewing the source of mongomodel
, here in particular:
https://github.com/spohlenz/mongomodel/blob/master/lib/mongomodel/support/configuration.rb#L45
To figure out what replicas should be I had to dig into the mongo
gem. Also worth noting, mongomodel
depends on a pretty old version of mongo
so I had to look back to 1.8.6, they are currently at 1.12.0. There I noticed that replicas where just strings of the hostname and port concatenated.
After reviewing the source I realized that all that need to change was a couple lines of my database.yml (wheelhouse.yml in wheelhouse world).
old config:
production:
database: database_name
host: some.host.com
port: 12345
username: username
password: password
new config:
production:
database: database_name
replicas:
- some.host.com:12345
- another.host.com:12345
username: username
password: password
Anyway, I think this should be simpler for someone to find when they first review the mongomodel readme and perhaps even when they generate the mongomodel.yml.
The ActiveModel dirty attribute methods (i.e. foo_changed?, foo_was, foo_change, reset_foo!) don't work. ActiveModel::Dirty stores changes in a Hash with String keys, but MongoModel::AttributeMethods::Dirty#write_attribute adds the changes with symbol attributes.
A fix with tests can be found here:
http://github.com/bdurand/mongomodel/commit/8ed71f994669c8ba1c7f8f8eccd1c84e3de530dd
The current type casting code won't let numeric fields be set to nil when set from an empty string. This is a problem in a web application when trying to set fields from the form params. A blank form field should indicate nil and not zero.
class MyModel < MongoModel::Document
property :value, Integer
end
model = MyModel.new(:value => "")
model.value # = 0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.