mongoid

undefined method page for #<Array:0xc347540> kaminari “page” error. rails_admin

半世苍凉 提交于 2019-12-08 03:45:27
问题 i am using rails_admin. when i go to certain resource. by typin url localhost:3000/admin/rule than it give me this error. code is: scope = Rule.all scope.page(1).per(2) . above code is writtten in rails_admin gem.in a file named mongoid.rb placed in adaptors folder. complete log is: NoMethodError (undefined method `page' for #<Array:0xcea7408>): mongoid (2.4.8) lib/mongoid/criteria.rb:385:in `method_missing' /home/usman/.rvm/gems/ruby-1.9.2-p290@system/bundler/gems/kaminari-809105ad782a/lib

Can rails scopes filter on the number of associated classes for a given field

我与影子孤独终老i 提交于 2019-12-08 01:55:02
问题 I am using Rails 3 with Mongoid. I have a Folder class that can then be shared with other User classes as such class Folder has_one :owner has_many :users I am trying to create two scopes one that can be used to return all private folders, and one to return all shared folders. Is there any way to count the number of associations within a scope? scope :personal, where(:users.count => 0) #Erroring on count... scope :shared, where(:users.count.gt => 0) #Erroring on count... I've considered

trigging after_save on an embedded item when assigning it via '<<' in mongoid?

匆匆过客 提交于 2019-12-08 01:41:08
问题 I was wondering if there was a way to trigger the after_save callback on an embedded_in object in Mongoid mapper. Example: i = Image.new(:file => file) user.images << i # => i.after_save should be triggered here I'm aware that if I call i.save after words, it will fire, however really hard to remember to do that throughout my code. Also, calling user.images.create(:file => file) is not an option, because I do a check to make sure the same file isn't uploaded twice. 回答1: The only real solution

Mongodb/Mongoid - what does {:multi => true} mean

主宰稳场 提交于 2019-12-08 01:38:43
问题 Regarding this question: With Mongoid, can I "update_all" to push a value onto an array field for multiple entries at once? I would like to ask: What's the purpose of {:multi => true} here? Is it possible to push a value into an array when update_all via mongoid now? because the question is in 2010. Thanks. 回答1: The documentation for the MongoDB update method states the following: multi - indicates if all documents matching criteria should be updated rather than just one. Can be useful with

Mongoid 3 - access map_reduce results

情到浓时终转凉″ 提交于 2019-12-07 23:43:41
问题 In mongoid 2, this used to work: mr_collection = self.collection.map_reduce(map, reduce, { :query => query, :finalize => finalize, :out => {:replace => 'mr_results'} }) limit = (options[:limit] || 10) skip = (options[:skip].to_i || nil) page = if skip >= limit ((skip+limit) / limit) else 1 end sort = if options[:sort_by_vintage] [['value.vy', :desc], ['value.s', (options[:sort] || :desc)], ['value.pml', :asc]] elsif options[:sort_by_sDate] [['value.sDate', :desc], ['value.s', (options[:sort]

Convert existing MongoDB string attribute to BSON::ObjectId

岁酱吖の 提交于 2019-12-07 19:27:30
问题 I have a collection of documents in MongoDB that has an attribute being stored as a string when it would work better if it was saved as BSON::ObjectId. The collection name is foo and the field is called bar . What's the best way to have every field bar turn its existing value into an instance of BSON::ObjectId? 回答1: Is this what you mean? (You have a string that is the hex of an ObjectId, and you want to turn it into an ObjectId) > db.foo.insert({bar:new ObjectId().str}); > db.foo.insert({bar

real polymorphism in embed relations in mongoid

不打扰是莪最后的温柔 提交于 2019-12-07 16:51:33
问题 Using Mongoid3, I'm trying to add polymorphism to my embedded relations. I have a class Item that must embed_one object containing my informations. The deal is that : - my object's type can be one of those : Calendar , Sticker , Picture ; - regardless my object's type, I want to access it by a unique 'key' : detail e.g. : pry> my_item1.detail => `<Picture _id: 1234>` pry> my_item2.detail => `<Sticker _id: 8964>` pry> First, I tried using the keywords as and polymorphic like described here:

Mongoid - querying by referenced document

馋奶兔 提交于 2019-12-07 14:58:51
问题 I have a model named Ad which looks like this: class Ad include Mongoid::Document referenced_in :category end and Category model: class Category include Mongoid::Document referenced_in :domain references_many :ads end How can I select Ads by domain? I have tried to use Ad.where('category.domain_id' => domain.id) but this does not work. 回答1: The problem is that MongoDB doesn't have any way of mapping a Category record to an Ad record. All it knows is that an Ad record has a category_id field

Mongoid - two fields inverses of the same foreign field

非 Y 不嫁゛ 提交于 2019-12-07 14:19:56
问题 I'm trying to get the following Mongoid relationships to work, but the game field of each team is an empty array. Is this not a valid relational model? Do I need to split up games, i.e. home_games and away_games? class Team include Mongoid::Document has_many :games, :autosave => true end class Game include Mongoid::Document belongs_to :home_team, :class_name => "Team", :inverse_of => :games belongs_to :away_team, :class_name => "Team", :inverse_of => :games end 回答1: I dont think so there is a

“Within X miles” search in mongodb

匆匆过客 提交于 2019-12-07 13:51:31
问题 I want to be able to find zip codes are that within a specific radius of distance from another zip code. I have some code from this source. But it is an SQL implementation using ActiveRecord. It is precisely the implementation I want but only with MongoDB. Help! 回答1: Take a look at the MongoDB documentation and the Mongoid indexing docs. class Zip include Mongoid::Document field :code field :location, :type => Array # make sure to rake db:mongoid:create_indexes index [[ :location, Mongo: