params[:user][:role_ids] ||= []
What does it do?
ruby -v = 1.9.2p290
params[:user][:role_ids] ||= []
What does it do?
ruby -v = 1.9.2p290
It assigns []
to params["user][:role_ids]
if params["user][:role_ids]
is nil
or another falsy value...
Otherwise, it retains the original value of params["user][:role_ids]
Example
variable = nil
variable ||= "string"
puts variable # "string"
variable2 = "value"
variable2 ||= "string"
puts variable2 # "value"
if params[:user][:role_ids]
is nil
, it gets initialized with []
otherwise params[:user][:role_ids]
holds its value further
If the left-hand value is not yet assigned, assign it to the right-hand value. If it is assigned, keep it as itself. A good explanation can be found on Michael Hartl's RoR tutorial site.
It's the memoize operator and it does one of two things:
It's a conditional assignment in Ruby. You can read more about it here: Ruby Operators
It sets a value to the variable if the variable isn't already set. Meaning
class Something
attr_accessor :some_value
def perform_action
@some_value ||= "Mom"
puts @some_value
end
foo = Something.new
foo.perform_action -> "Mom"
foo.some_value = "Dad"
foo.perform_action -> "Dad"