mongoid

Deleting a document in monogid after sometime

牧云@^-^@ 提交于 2020-01-17 11:15:34
问题 I am working on rails app with mongoid. This is one of model(collection): class Document include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paperclip field :time, type: Time field :radius, type: Float attr_accessible :time,:radius end The time field contain the actual time when model should be deleted. How can I do that , One idea I have to write a script put that in cron job. But I don't want to create a cron job. Is there any other way I can automate this or any method

Mongoid and carrierwave

爱⌒轻易说出口 提交于 2020-01-16 04:14:06
问题 In order to remain DRY, I have a class ModelBase that includes Mongoid document as follows: class ModelBase include Mongoid::Document alias_attribute :guid, :id def as_json(options = {}) azove_hash = options.merge(:methods => :guid) super azove_hash end end then all my models inherit from ModelBase and they seem to be working fine. However, there is one model where I use CarrierWave. when it inherits from ModelBase, the call to mount_uploader fails. And when I include the model inside with no

undefined method `valid_options' for nil:NilClass with simple_form and Mongoid

自古美人都是妖i 提交于 2020-01-15 05:40:27
问题 I have two models, Category and Post. Category.rb class Category include Mongoid::Document field :title, :type => String has_many :posts, :autosave => true, dependent: :destroy end Post.rb class Post include Mongoid::Document field :title, :type => String belongs_to :category end I'm using simple_form gem If I write in my post form the next: <%= simple_form_for(@post) do |f| %> <%= f.collection_select :category, Category.all, :id, :title, :prompt => "Choose a Category"%> <%= f.input :title %>

Mongoid gives uninitialized constant Mongo

为君一笑 提交于 2020-01-15 03:31:31
问题 I'm trying to use mongoid but it outputs this error: uninitialized constant 'Mongo' Here is my code: require "mongoid" Mongoid.configure do |config| config.master = Mongo::Connection.new("localhost",27017).db("arthist") end class Artist include Mongoid::Document field :name, type: String end a = Artist.create(name: "hoge") Do you have any idea? 回答1: include gem mongo in your Gemfile and restart the server it should do the trick. 来源: https://stackoverflow.com/questions/12188219/mongoid-gives

Ember model only loads last record

时间秒杀一切 提交于 2020-01-14 13:36:30
问题 I'm trying to reproduce Railscasts 410 example (called Raffler ), changing the setup for last versions and to match my habits: Ember 1.0.0-rc.6 Rails 4.0.0 Mongoid master (4.0) Haml 4 Emblem 0.3.0 In this example project, we create a simple model Entry that calls a small Rails Rest API. Everything works as expected, except that calling Raffler.Entry.find() to get all entries only loads the last record. Here is my model : Raffler.Entry = DS.Model.extend name: DS.attr('string') winner: DS.attr(

Ember model only loads last record

Deadly 提交于 2020-01-14 13:36:10
问题 I'm trying to reproduce Railscasts 410 example (called Raffler ), changing the setup for last versions and to match my habits: Ember 1.0.0-rc.6 Rails 4.0.0 Mongoid master (4.0) Haml 4 Emblem 0.3.0 In this example project, we create a simple model Entry that calls a small Rails Rest API. Everything works as expected, except that calling Raffler.Entry.find() to get all entries only loads the last record. Here is my model : Raffler.Entry = DS.Model.extend name: DS.attr('string') winner: DS.attr(

Rails form to edit JSON object as text

你。 提交于 2020-01-14 06:25:11
问题 I'd like to make a form that lets a user edit one field of a mongoid object as rendered JSON text. There's a field in the model that my rails app should not understand, but I want to expose a generic editor. So for this field, I'd like to render it as pretty JSON, and expose it in a big <textarea> and then parse the JSON back in after any edits. I can think of a dozen ways to do this, but I'm wonder what would be most consistent with Rails philosophy and least divergent from normal

Rails 3 using MongoDB via mongoid adapter - is there any way to share attribute specifications without using Single-Table Inheritance?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-13 18:20:07
问题 Probably a confusing title, but not sure how else to put it. Example should make it clearer. I have many different models that share many of the same attributes. So in each model I have to specify those same attributes and THEN the attributes that are specific to that particular model. Is there any way I can create some class that lists these basic attributes and then inherit from that class without using Single-Table Inheritance? Because if I put all the shared attributes and Mongoid

How to call to mongodb inside my map/reduce functions? Is it a good practice?

北慕城南 提交于 2020-01-13 17:57:19
问题 I would like to know if: Firstly: Is it possible to use mongodb functions inside my map/reduce functions, for example: function() { foo = db.myCollection.find({ _id: ObjectId('4ee235ce002c62f393000008')}) print(foo); # returns 'db.myCollection -> undefined' } Secondly: Is it a good practice? For example, I need to map a specific property from the documents referenced for a 'root' document. Or maybe, can I set a habtm relationship on this specific property? Thanks! 回答1: although it is possible

Find all the mongoid model names in my application

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-13 11:04:46
问题 Is there a way to find out all the Mongoid Models names in my rails app. I can find all the models just by getting all the file inside my app/models folder but i specifically want mongoid model names. 回答1: If your model classes are already loaded then you could list them by finding all the classes that include the Mongoid::Document module. Object.constants.collect { |sym| Object.const_get(sym) }. select { |constant| constant.class == Class && constant.include?(Mongoid::Document) } or if you