With the Rails expression dependent: :destroy
, is dependent
the hash key and destroy
just a symbol?
Asked
Active
Viewed 447 times
0
-
This is pretty much the same as [**Is there any difference between the `:key => “value”` and `key: “value”` hash notations?**](http://stackoverflow.com/q/8675206/479863), isn't it? Not quite a duplicate but fairly close. – mu is too short Apr 13 '16 at 23:46
2 Answers
1
The notation introduced in Ruby 1.9 is just a shortcut, and you can see what it means using irb
:
h = { dependent: :destroy }
# => { :dependent => :destroy }
They're both symbols. Don't forget that a hash can be keyed by any object, not necessarily a symbol or a string. This is completely different from most languages where the key will be coerced into something consistent.
Using that example you can see what the types of the keys and values are:
h.keys
# => [:dependent]
h.values
# => [:destroy]
They're all symbols in this case.

tadman
- 208,517
- 23
- 234
- 262
0
Writing has_many :orders, dependent: :destroy
is the same as has_many(:orders, {:dependent => :destroy})
:dependent
is the key, :destroy
the value of the hash passed as an argument to has_many
.

magni-
- 1,934
- 17
- 20