Which syntax is preferred to create a Hash in Ruby on Rails? I am writing some code in Ruby on Rails to Create an object.

I am using Ruby 2.0 and Rails 4.0.3 in my Application.

I have a Model called:

class GroupUsers &lt; ActiveRecord::Base
  belongs_to :users
  belongs_to :group


This is basically a ManyToMany mapping between Users and Groups.
So, today I was creating an object for this Model.

But I found out I could follow two syntax for this, as follows

GroupUsers.create( :user =&gt; user, :group =&gt; group )


Or

GroupUsers.create( user: user, group: group )


Basically since this is related to creating a Hash so it must be a question of the best practice in Ruby. 

Which syntax should be preferred ?
It depends. I prefer the newer syntax because I feel that it is more readable. As others have pointed out, however, the newer syntax is only compatible with Ruby 1.9.2+.

You should be aware, however, that the new syntax does not entirely replace the hash rocket syntax. This is because you can only use the newer syntax with symbols.

{ "text": :hello } #=&gt; syntax error, unexpected ':', expecting =&gt;


If you want to use strings and methods, you will still need to use the has rocket:

{ method_name =&gt; :hello }
{ "a_string" =&gt; :hello }


Personally, from my experience of reading and watching educational materials, the newer syntax is preferred where possible. It's also less keystrokes: : instead of =&gt; is easier for me.