Menu
  • HOME
  • TAGS

Mongoid: How to validate value of specific key on Hash field?

ruby-on-rails,ruby-on-rails-3,mongodb,mongoid,mongoid3

Since Hash data type is unstructured hash value (i.e. it doesn't have any reference to the keys inside the hash, you can literally stick any hash string in there), you will have to write a custom validation method: validates :details_has_full_name def details_has_full_name errors.add(:details, "Some Error Message") if details["fullName"].nil? end Note:...

Atomic inc of multiple mongoid hash values

ruby-on-rails,ruby,ruby-on-rails-3,mongoid,mongoid3

The .inc method works perfectly with non-existing keys, but the document is not automatically refreshed from the database. Try: dimension_stat.inc('data.a' => -1, 'data.b' => 5) puts dimension_stat.data['a'] # not changed dimension_stat.reload puts dimension_stat.data['a'] # changed you will see that the counters have changed....

MongoDB with MongoID Fetching few fields in Find Query

ruby-on-rails-3,mongoid3

Using mongo itself, it will return null values. Have you tried using the pluck method in mongoid 3 ? Collection.all.pluck(:username) ...

Process Hashes in Document Collections Mongoid

ruby-on-rails,mongodb,hash,mongoid3

For the desired communities get the admins, then you can do if admins.include?(current_user) # treat as admin end Edit all_admins = Array.new Get all commmuniteis @communities = Community.all @communities.each do |c_comm| all_admins.concat(User.where(:id.in => c_comm.special_user_ids.collect { |k, v| k })) end puts all_admins.include(current_user)? ...

Mongoid nested embedded document save ignores query

ruby,mongoid,mongoid3,mongoid4

Mongoid is quickly forcing me to become an alcoholic. Hopefully it will help someone in the same situation. class Instrument include Mongoid::Document embedded_in :member field :name, type: String def make_badass self.name = "Badass #{self.name}" self.member.band.save end def unset_name # self.unset :name does not work self.remove_attribute :name self.member.band.save end end ...

Undefined method `reset_counters' for Mongoid 3.1.6

ruby-on-rails,mongoid,mongoid3,counter-cache

Looks like you just have a namespace problem. If you look at the error closely: undefined method ... for Mongoid::Persistence::Atomic::Operation:Module you'll see that it is complaining about not being able to find reset_counters in Mongoid::Persistence::Atomic::Operation, not Operation. And if you look at the 3.1.6 source, you'll find Operation in lib/mongoid/persistence/atomic/operation.rb....

two methods for after_add callback on mongoid relations

mongoid,mongoid3

try this has_and_belongs_to_many :posts, after_add: [:method1, :method2] ...

adding allowDiskUse parameter to db.collection.aggregate() query using Mongoid

ruby,ruby-on-rails-3,mongodb,mongoid,mongoid3

The problem is that Moped does not currently permit options for Moped::Collection#aggregate, just a pipeline for args, as can be seen here: https://github.com/mongoid/moped/blob/master/lib/moped/collection.rb#L146 - the Mongo Ruby driver supports options for Mongo::Collection#aggregate, but Mongoid 3 uses Moped for its driver. However, thanks to the dynamic nature of Ruby, you can...