mongoid

How to enforce unique embedded document in mongoid

吃可爱长大的小学妹 提交于 2019-12-23 07:38:32
问题 I have the following model class Person include Mongoid::Document embeds_many :tasks end class Task include Mongoid::Document embedded_in :commit, :inverse_of => :tasks field :name end How can I ensure the following? person.tasks.create :name => "create facebook killer" person.tasks.create :name => "create facebook killer" person.tasks.count == 1 different_person.tasks.create :name => "create facebook killer" person.tasks.count == 1 different_person.tasks.count == 1 i.e. task names are unique

mongoid multi parameter attributes problem with date

随声附和 提交于 2019-12-23 06:56:43
问题 I have been following the mongoid railscast and I am facing a multi parameter attributes problem when i add a new field field :published_on, :type => Date Article.find("4da14b1447640b14eb000002").published_on => nil but with Article.find('4da14b1447640b14eb000002') => published_on: nil, published_on(1i): "2011", published_on(2i): "6", published_on(3i): "10" How do i get the desire output? i know there is a temporary solution https://gist.github.com/315227 but the issue of this problem is

Translating mongoid to german

試著忘記壹切 提交于 2019-12-23 04:08:33
问题 I got a problem to translate the default validation messages in mongoid. I create a /config/initializers/mongoid.rb with Mongoid.add_language("de") http://mongoid.org/docs/installation/languages.html But every call on a validated field i got translation errors: translation missing: de.mongoid.errors.models.comment.attributes.body.too_short translation missing: de.mongoid.errors.models.user.attributes.username.blank I have Ruy on Rails 3.2 Ruby 1.9.3 mongoid 2.4.7 回答1: If you check on Mongoid

Is it possible to change default TimeZone in MongoDB using Rails 3?

孤街浪徒 提交于 2019-12-22 23:20:23
问题 I have such trouble: when I'm creating object and setting some datetime It is saving database in UTC TimeZone. Here is example: //showing full list of object properties Grant _id: 5108ee29e6b564611400000, start_date: 2013-01-30 09:56:27 UTC //then showing a.start_date Wed, 30 Jan 2013 13:56:27 +0400 I tried to forbid database to use UTC. Here is mongoid.yml: development: options: raise_not_found_error: false sessions: default: use_activesupport_time_zone: true use_utc: false database: test

How do you seed relationships for Mongoid in Ruby on Rails?

拟墨画扇 提交于 2019-12-22 22:51:12
问题 I'm trying to create a small game server using Ruby on Rails, Mongo, with Mongoid as the ORM, with Devise for authentication. I'm trying to modify the db/seeds.rb to seed several users and game documents. How do you create a seed between two Mongo/Mongoid relationships? I have Users and Games . Users have_many Games. I've found examples of creating a seed database for "embeds_many" and "embedded_in", but not for has / belongs. A follow-up would be if this is the proper architecture (there's a

How to benchmarking mongodb/mongoid scripts, for comparing two different query techniques

我的梦境 提交于 2019-12-22 18:36:08
问题 Do you have a suggestion on how to test performances over two different mongoid/mongodb query implementations? The implementation to compare, are related to a previous "Q & A" but I'll report those here again, for brevity : first code : Competitor = Struct.new(:html_url, :description, :user) competitors = [] User.all.map do |user| user.watchlists.all.map do |wl| if wl.tags_array == ["ruby", "web", "framework"] competitors << Competitor.new(wl.html_url, wl.description, wl.user.nickname) end

Efficient way to store data in MongoDB: embedded documents vs individual documents

流过昼夜 提交于 2019-12-22 18:15:13
问题 I store user activity data: when user visited current article, topic or personal message to show him how many new comments and messages were added while he was offline. class SiteActivity include Mongoid::Document include Mongoid::Timestamps belongs_to :user belons_to :activity, polymorphic: true end In this case I store one record per document. Another option is to use embedded documents, so all user activities will be stored in one document: class SiteActivity include Mongoid::Document

Ruby on Rails: Concatenate results of Mongoid criterias and paging

北战南征 提交于 2019-12-22 11:35:32
问题 I'm pretty sure that I'm doing something wrong. Consider the following code: criteria1 = Model.where(...) criteria2 = Model.where(...) results = (criteria1.to_a + criteria2.to_a)[offset..(offset + count_per_page - 1)] This code concatenates results of two different criterias and get a certain number of results with a given offset (paging). The problem in this code is implicit. The to_a method call actually loads all results of a criteria to the memory as an array . Now consider a really huge

Reduce the execution time of jobs of sidekiq

大兔子大兔子 提交于 2019-12-22 11:19:03
问题 I am currently working on an app which involves syncing of contacts on rails server. I am using redis server and sidekiq for performing contact syncing in the background. My database is mongodb and I am using mongoid gem as ORM. Workflow is a follows: Contacts on the phone are passed to the rails server through app and then on the rails server, it is queued in the redis server. Now cron job triggers sidekiq which connects to redis and completes the job. One Job of sidekiq is as follows: it

Get raw mongo db query expression that mongoid generate it

核能气质少年 提交于 2019-12-22 10:16:05
问题 I want to get the mongoid generated mongo query expression how to do it ? e.g. this is the mongoid syntax History.where(report_type: params["report_type"]).order_by(ts:1).only(:ts).last I want to have the method, such as to_sql , to get the native query expression so that I can apply it on Mongo console. 回答1: MongoDB doesn't really have a query language like SQL so you can't get the whole thing in one nice compact piece. You can, however, get the pieces. This: History.where(report_type: