DateTime with MongoDB/Mongoid and Rails 3 Not Populating
Mongoid does not handle multiparameter attributes like Date yet, so you need to do the following:
# copied from: https://gist.github.com/315227
# add this to a new file in your lib directory
module MultiParameterAttributes
def filter_time(attributes, name)
attrs = attributes.collect do |key, value|
if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
[$1.to_i, value.send("to_#$2")]
end
end.compact.sort_by(&:first).map(&:last)
Time.zone.local(*attrs) unless attrs.empty?
end
end
# include the module above in your application_controller.rb
class ApplicationController < ActionController::Base
include MultiParameterAttributes
end
# and in the controller action where you process the form params, use filter_time
class YourController < ApplicationController
def your_action
time = filter_time(params, :my_time_attribute_name)
end
end
Some more info here:
http://groups.google.com/group/mongoid/browse_thread/thread/f83cbdd641581912
Mongoid document Time to Live
MongoDB (version 2.2 and up) actually has a special index type that allows you to specify a TTL on a document (see http://docs.mongodb.org/manual/tutorial/expire-data/). The database removes expired documents for you--no need for cron jobs or anything.
Mongoid supports this feature as follows:
index({created_at: 1}, {expire_after_seconds: 1.week})
The created_at
field must hold date/time information. Include Mongoid::Timestamps
in your model to get that for free.
UPDATE:
If you want to expire only a subset of documents, then you can create a special date/time field that is only populated for that subset. Documents with no value or a non-date/time value in the indexed field will never expire. For example:
# Special date/time field to base expirations on.
field :expirable_created_at, type: Time
# TTL index on the above field.
index({expirable_created_at: 1}, {expire_after_seconds: 1.week})
# Callback to set `expirable_created_at` only for guest roles.
before_create :set_expire, if: "role == :guest"
def set_expire
self.expirable_created_at = Time.now
return true
end
Emulating join behavior with Rails and Mongoid
I will start from the end:
I guess the attractiveness of mongodb is its javascript syntax and
similarity to json :)
Not only this, and json style is not main advantage. Main advantages of mongodb is ability to embedd documents, high performance and full scalability, full index support, map/reduce, etc.
So if I wanted flexibility and needed my data to join multiple times,
should I go with Postgresql?
It depends on concrete task, for example if you designing report system i prefer to use some relational database. But sometimes instead of joins and separate collections you can embedd documents + mongodb good fit for the data denormalization ( and in many situations you can denormalize in background to avoid joins )
I know mongodb has fast reads / writes but needs to query multiple
times to emulate joins. So when would this become a performance hit?
If you will use mongodb as regular relational database (without embedding and denormaliztion) you never achieve best performance.
Does mongodb limit your ability to create new complex relationships on
your data that did not previously exist?
No mongodb not limit you, because of it does not contains any constraints between collections like foreign key in any sql database + it allow embedd and easy denormalize data to fit your business needs and achieve best performance.
Rails 4 & Mongoid : date helpers
Arf, just found this question that mentions it has been removed in mongoid 4. I'll use this gem then
https://github.com/netguru/mongoid-sadstory
But if you have other alternatives....
Timestamp not updating in RoR application
My guess would be that you need to change the default value into a Proc.
In development mode, your models are being reloaded every request, so DateTime.now
is always current. In production, however, the class is only loaded once (per dyno) during app startup, and DateTime.now
results in a static value.
field :added_on, :type => DateTime, :default => Proc.new { DateTime.now }
should be what you want.
Rails 4/Devise/MongoDB: Unpermitted parameters using custom properties and strong parameters
I had the exact same issue and overriding sign_up_params did work for me
def sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation, :other, :etc)
end
of course, the difference is in that mine are just scalar values, while you're trying to mass assign a relation... I guess that's where you should look for.
By the way, the documentations is still inexistint in this topic (too new), and code commnents suggest to override devise_parameter_sanitizer, which isn't necessary.
Related Topics
Run a Cli Thor App Without Arguments or Task Name
Create Hash from Array and Frequency
Need Advice: Is This a Good Use Case for a 'Nosql' Database? If So, Which One
How to Resolve Deprecation Warnings for Openssl::Cipher::Cipher#Encrypt
Setting Up Env, Osx Rbenv and Bundle Battle
Accessing One Controller Variable in Another Controller in Rails
Can't Launch Simple Sinatra App Using Rackup and Jruby (No Response from Web Server)
Import SASS Partial Over Http Instead of Filesystem
How to Enter Password in a Popup Using Watir
When and Where Do I Require Files in a Rails Application
How to Use the Ruby "Self" Keyword
Rspec View Undefined Method Stub_Model
Are There Any Good Mutation Testing Tools for Ruby 1.9 and Rspec2
How to Run a Simple Ruby Script in Any Web Server (Apache or Mongrel or Any Thing Else)