In Ruby on Rails, what is the difference between
:bonus_card
and
dependent: ?
(in the example
class TrainPassengers < ActiveRecord::Base
has_one :bonus_card, dependent: :destroy
end
)
In Ruby on Rails, what is the difference between
:bonus_card
and
dependent: ?
(in the example
class TrainPassengers < ActiveRecord::Base
has_one :bonus_card, dependent: :destroy
end
)
dependent: :destroy
is alternate syntax for:
:dependent => :destroy
It's new in Ruby 1.9.
:dependent
, :destroy
and :bonus_card
are all symbols.
it is new syntax in ruby 1.9
dependent: :destroy
which is alternative of this
:dependent => :destroy
i think you are new in ruby because experienced person has idea about it and if you scaffold then dependent: :destroy this code is generated by rails by default now.
:dependent ,:destroy and :bonus_card are all symbols.Read about symbols these are very usefull
In Ruby, :something
is a symbol. A symbol is a reference kept in memory and looked up quickly, so that it is a nice data type to use as hash keys. A Ruby Hash usually looks like:
{ :some_key => "some data", :other_key => 65536, :yet_another_key => :symbol_as_data }
Ruby 1.9 introduced a shorthand notation for writing the above, as long as keys are symbols, you can move the colon to the end and omit the fat arrow =>
{ some_key: "some data", other_key: 65536, yet_another_key: :symbol_as_data }
This is only valid for symbols passed as hash keys (also in hashes passed as method parameters). Other objects used as hash keys must still use the fat arrow(=>
), and symbols used elsewhere are always written colon-first.