mongoid

How can I find records by “count” of association using rails and mongoid?

只愿长相守 提交于 2019-12-20 11:09:49
问题 With these models: class Week has_many :proofs end class Proof belongs_to :week end I want to do something like: Week.where(:proof.count.gt => 0) To find only weeks that have multiple proofs. There is one answer that seems to address this: Can rails scopes filter on the number of associated classes for a given field But in this example, there is no such attribute as proof_ids in Week since the ids are stored with the proofs. This does not work for example: Week.where(:proof_ids.gt => 0) How

Mongo DB Design, embedding vs relationships

匆匆过客 提交于 2019-12-20 10:25:33
问题 I'm building a simple accounting system where a user has many bills. Now I'm trying to decide if bills should be its own collection, or nested within the user. I'm leaning towards the former but I've NEVER done any noSQL stuff so I'm just going by trial and error and what I think makes sense to me. I understand that Mongo has a 4mb document size limit which is what's making me think that I should have a separate collection for bills, as these will accumulate daily and could eventually take up

How to reference an embedded document in Mongoid?

我是研究僧i 提交于 2019-12-20 10:08:18
问题 Using Mongoid, let's say I have the following classes: class Map include Mongoid::Document embeds_many :locations end class Location include Mongoid::Document field :x_coord, :type => Integer field :y_coord, :type => Integer embedded_in :map, :inverse_of => :locations end class Player include Mongoid::Document references_one :location end As you can see, I'm trying to model a simple game world environment where a map embeds locations, and a player references a single location as their current

How to reference an embedded document in Mongoid?

眉间皱痕 提交于 2019-12-20 10:07:01
问题 Using Mongoid, let's say I have the following classes: class Map include Mongoid::Document embeds_many :locations end class Location include Mongoid::Document field :x_coord, :type => Integer field :y_coord, :type => Integer embedded_in :map, :inverse_of => :locations end class Player include Mongoid::Document references_one :location end As you can see, I'm trying to model a simple game world environment where a map embeds locations, and a player references a single location as their current

What inverse_of does mean in mongoid?

ぐ巨炮叔叔 提交于 2019-12-20 08:57:44
问题 What inverse_of does mean in mongoid associations? What I can get by using it instead of just association without it? 回答1: In a simple relation, two models can only be related in a single way, and the name of the relation is automatically the name of the model it is related to. This is fine in most cases, but isn't always enough. inverse_of allows you to specify the relation you are referring to. This is helpful in cases where you want to use custom names for your relations. For example:

How can I disable MongoDB log messages in console?

戏子无情 提交于 2019-12-20 08:39:37
问题 I have this little test script: require 'mongo' mongo_client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test') mongo_client[:collection].insert_one({a: 1}) An this is the console output: $ ruby test.rb D, [2015-05-17T21:12:05.504986 #25257] DEBUG -- : MONGODB | Adding 127.0.0.1:27017 to the cluster. | runtime: 0.0212ms D, [2015-05-17T21:12:05.531238 #25257] DEBUG -- : MONGODB | COMMAND | namespace=admin.$cmd selector={:ismaster=>1} flags=[] limit=-1 skip=0 project=nil | runtime:

Pros and cons of using callbacks for domain logic in Rails

风流意气都作罢 提交于 2019-12-20 08:10:20
问题 What do you see as the pros and cons of using callbacks for domain logic? (I'm talking in the context of Rails and/or Ruby projects.) To start the discussion, I wanted to mention this quote from the Mongoid page on callbacks: Using callbacks for domain logic is a bad design practice, and can lead to unexpected errors that are hard to debug when callbacks in the chain halt execution. It is our recommendation to only use them for cross-cutting concerns, like queueing up background jobs. I would

Pros and cons of using callbacks for domain logic in Rails

可紊 提交于 2019-12-20 08:09:12
问题 What do you see as the pros and cons of using callbacks for domain logic? (I'm talking in the context of Rails and/or Ruby projects.) To start the discussion, I wanted to mention this quote from the Mongoid page on callbacks: Using callbacks for domain logic is a bad design practice, and can lead to unexpected errors that are hard to debug when callbacks in the chain halt execution. It is our recommendation to only use them for cross-cutting concerns, like queueing up background jobs. I would

Method works in development but not production Rails MongoDB

左心房为你撑大大i 提交于 2019-12-20 07:33:02
问题 I have a Coupon class and I want my app to check and see how many counts are left on the coupon and if the date for the coupon has expired. I have the following method in my class to check both of these. Coupon class def self.get(code) where( :code => (normalize_code(code)), :$and => [ { :$or => [ { :coupon_count.gte => 1 }, { :coupon_count => nil } ] }, { :$or => [ { :expires_at.gt => Time.now.utc }, { :expires_at => nil } ] } ] ).first end This works fine in development when I enter a

Method works in development but not production Rails MongoDB

£可爱£侵袭症+ 提交于 2019-12-20 07:31:06
问题 I have a Coupon class and I want my app to check and see how many counts are left on the coupon and if the date for the coupon has expired. I have the following method in my class to check both of these. Coupon class def self.get(code) where( :code => (normalize_code(code)), :$and => [ { :$or => [ { :coupon_count.gte => 1 }, { :coupon_count => nil } ] }, { :$or => [ { :expires_at.gt => Time.now.utc }, { :expires_at => nil } ] } ] ).first end This works fine in development when I enter a