Rails mongodb - set today's date in field :date

mongodb query today
mongodb current date
mongodb current date aggregation
mongodb insert current date
mongodb query date without time
mongodb timestamp to date
mongodb $$now
date mongodb

I'd like to automaticaly add the today's date for the :date field of my controller. I'm new with Rails and have no idea how to set this default value, except that it must be in the controller.

Here is my Post model :

class Post
  include Mongoid::Document
  field :title, type: String
  field :text, type: String
  field :date, :type => DateTime
end

And my Post controller :

def create
  @post = Post.new(post_params)

  @post.save
  redirect_to @post
end 

private
  def post_params
    params.require(:post).permit(:title, :text, :date)
end

Looking in Rails docs, I read about Date.current or Date.today.

Setting default values in your class, as @T J suggested would do the trick.

But if you want to set the default value only in an specific view, let's say your 'posts/create' view, you could use this in your correspoding action:

    def create
      @post = Post.new(post_params)
      @post.date ||= Date.today
      .
      .
      .
    end

The syntax:

     @post.date ||= Date.today

is a short cut for

    @post.date = @post.date || Date.today

$currentDate, a boolean true to set the field value to the current date as a Date, or; a document { $type: "timestamp" } or { $type: "date" } which explicitly specifies the type. In this article, I am going to show you how to setup schema-less database MongoDB with your Rails 5 application. I am using Ubuntu 14.04 , Ruby 2.5.1 and Rails 5.2 for this tutorial. If you have not installed MongoDB on your machine, then you first need to install it.

You could always let Mongoid handle the default value for you

class Post
  include Mongoid::Document
  field :title, type: String
  field :text, type: String
  field :date, :type => DateTime, default: ->{ Date.today }
end

This way, if date is nil in your params, it will get set to the todays date.

Default values are documented here.

Date(), If no document with _id equal to 1 exists in the products collection, the following operation inserts a document with the field dateAdded set to the current date:. Setting Up Rails 4 with MongoDB and Mongoid Overview. If you've never checked out MongoDB before, I'd very much encourage you to. It's a NoSQL document store that makes for a very interesting method of development compared to relational databases.

I know this is an old post, I'd like to add that in some cases you can use the Timestamps module by simply including to your model:

include Mongoid::Timestamps::Created

Querying, A Ruby ODM for MongoDB. All queries in Mongoid are Criteria , which is a chainable and lazily evaluated Perform a $set on all matching documents. This Week in MongoDB June 17-23. Here's what's going on in the MongoDB community this week: Learn More Free Online Class, MongoDB for Developers, Begins Today See what happens when you have MongoDB & Hadoop, Sittin' in a Tree with @mortardata at MongoNYC Fill out the MongoDB Developer Survey Upcoming MongoDB Days June 21: MongoDB New York City July 2: An Evening with MongoDB Sao Paulo July 2

Mongoid: Documents, The representation of a Document in MongoDB is a BSON object that is very similar For Rails applications, set the fallbacks configuration setting to true in your  5.0.0 introduces Rails 4.0 and 4.1 support and Mongoid 4 support alongside the existing Rails 3.1, 3.2, and Mongoid 3 support. Ruby 1.8.7 support is dropped. The database is no longer set automatically for the MongoStore when MongoMapper or Mongoid is present. You have to set the database manually whenever you choose to use the vanilla MongoStore.

Setting Up Rails 4 with MongoDB and Mongoid, Today we're going to walk through installing it on Ubuntu and setting up Rails with MongoDB and Mongoid to replace ActiveRecord. Installing MongoDB. First let's  # The name of the replica set to connect to. Servers provided as seeds that do # not belong to this replica set will be ignored. # replica_set: name # Whether to connect to the servers via ssl. (default: false) # ssl: true # The certificate file used to identify the connection against MongoDB. # ssl_cert: /path/to/my.cert

Learning Rails 5: Rails from the Outside In, Rails models, at least when you first set them up. Note NoSQL databases are an alternative approach in Rails. Tools like Firebase, CouchDB, MongoDB, and  Install on Amazon. Install using .tgz Tarball. Install on macOS. Install on Windows. Install using msiexec.exe. Install with Docker. Upgrade MongoDB Community to MongoDB Enterprise. Upgrade to MongoDB Enterprise (Standalone) Upgrade to MongoDB Enterprise (Replica Set) Upgrade to MongoDB Enterprise (Sharded Cluster)