I have model:
class Task
include Mongoid::Document
field :name, type: String
field :category, type: String
# ....
And I have a method to create new documents:
def self.create_task(hash)
Task.safely.create!(name: hash["name"],
category: hash["category"],
... );
I’m just wondering if it is possible to simplify this code and pass Ruby Hash to create!() method of Mongoid::Document ?
Something like this:
Task.safely.create!(hash)
This:
is exactly the same as this:
which is the same as this:
So you can probably just do this:
When you do something like this:
Ruby notices that you’re supply a Hash literal without the usual
{...}wrapping and adds it for you; this sort of thing works at the end of any argument list:is the same as: