mongoid

Mongo::Collection undefined method `insert'

走远了吗. 提交于 2019-12-12 06:23:56
问题 I'm trying to import millions of rows from another database into MongoDB. My routine for import uses MyModel.collection.insert(data_to_import) And I get NoMethodError: undefined method `insert' for #<Mongo::Collection:0x000000082bb990> /home/mika/projects/ca2/lib/tasks/data.rake:36:in `block (2 levels) in <top (required)>' /home/mika/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval' /home/mika/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>' The model is defined with

Finding distinct rows in scope with mongoid

…衆ロ難τιáo~ 提交于 2019-12-12 05:30:01
问题 I have two models in my rails project: persons and an embeded collection, cities. there is also a spatial index on the cities. I want to find all persons near one city. class Person include Mongoid::Document include Mongoid::Timestamps embeds_many :cities scope :nearby, ->(location, distance) { where('cities.location' => {'$near' => location , '$maxDistance' => distance})} class City include Mongoid::Document include Mongoid::Timestamps embedded_in :person field :city field :location, type:

How protect all fields against mass assignment in mongoid app

孤街醉人 提交于 2019-12-12 04:57:59
问题 I have added this fix https://gist.github.com/2382288 for protect all fields against mass assignment in mongoid app. in my config/initializers/mongoid.rb I have added this fix: module Mongoid module MassAssignmentSecurity extend ActiveSupport::Concern included do attr_accessible nil end end module Document include MassAssignmentSecurity end end My question is: this fix completely protects your application against attacks mass assignment? Or is recommended to add attr_accessible all the

Mongoid virtual attributes in to_json

拜拜、爱过 提交于 2019-12-12 03:55:48
问题 I'm trying to get some virtual (non-persisted) attributes to show up in the JSON representation of some Mongoid models, but can't seem to get it to work: class MyModel include Mongoid::Document def virtual_attribute @my_attribute || false end def virtual_attribute=(value) @my_attribute=value end end class MyController def myaction false_values=MyModel.where( whatever ) true_values=MyModel.where( something_else ).map{ |model| model.virtual_attribute=true } @val['my_models']=false_values+true

Mongoid Grouping by date

拜拜、爱过 提交于 2019-12-12 03:27:12
问题 this is the kind of document I have: { event_type: 'click', created_at: '2015-02-15 10:00:00', user_id: 100 } I need to sum unique users per day with event_type = click. To get something like: { count: 320, date: '2015-02-15' } { count: 451, date: '2015-02-16' } (note the date format) I've tried many different things I found on SO, this one almost worked for me: Best way to group by date with Mongoid I just couldn't figure out how to modify it to do exactly what I need. This is the raw query

Mongoid Association Creating (unwanted) Records

若如初见. 提交于 2019-12-12 03:23:15
问题 I'm at a loss to why Mongoid is creating a new record in an association. I'm stepping closely through the code, but I've never seen anything like this. I've made a test and slimmed down the code. I left the VCR in just in case it might be related. it "should not create a duplicate entry for MT" do state = PolcoGroup.create(type: :state, name: 'MT', active: true) s = state.get_senators state.junior_senator = s[:state_junior_senator] # !!!!! this creates a new record state.senior_senator = s[

Mongoid sort Model based on array size which is in other Model (has_one) relation

会有一股神秘感。 提交于 2019-12-12 03:09:53
问题 I have Postactivity model, which has post_id as the foreign key of the Post Model (has_one relation) and this Postactivity model has the likes array. How i can sort Post model by likes ? class Post has_one :postactivity, foreign_key: :post_activity_id, class_name:"PostActivity" end class PostActivity field :likes, type: Array belongs_to :post, foreign_key: :post_id, class_name: "Post" end 回答1: class PostActivity field :likes, type: Array field :likes_count, type: Integer, default: 0 belongs

Rails g failing for mongoid commands

你。 提交于 2019-12-12 03:03:27
问题 I am trying to initialise mongoid from a rails 3.2 application using ruby-1.9.2 with the following command: rails g mongoid:config When I issue the command, I get the following error: /Users/paulcowan/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/rubygems_integration.rb:143:in block in replace_gem': railties is not part of the bundle. Add it to Gemfile. (Gem::LoadError) from /Users/paulcowan/.rvm/gems/ruby-1.9.2-p290/bin/rails:18:in ' My gem file looks like this: source 'https:/

Calling Document.find with nil is invalid MongoId in Rails 4

佐手、 提交于 2019-12-12 01:54:14
问题 There are three models in my app: User, Subject and Note. I've already embedded Note model into Subject model and it works. Now when I try to embed Subject into User always get Mongoid::Errors::InvalidFind coming from this line of code: In the method one should define to use in before_action at the beginning of the controller @user = User.find(params[:user_id]) subjects_controller class SubjectsController < ApplicationController #before_filter :authenticate_user!, only: [:index] before_action

Rails: use existing model validation rules against a collection instead of the database table

孤街醉人 提交于 2019-12-12 01:45:27
问题 Rails 4, Mongoid instead of ActiveRecord (but this should change anything for the sake of the question). Let's say I have a MyModel domain class with some validation rules: class MyModel include Mongoid::Document field :text, type: String field :type, type: String belongs_to :parent validates :text, presence: true validates :type, inclusion: %w(A B C) validates_uniqueness_of :text, scope: :parent # important validation rule for the purpose of the question end where Parent is another domain