Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

As someone who doesn't know Rails, the following code seems odd to me:

  class Team < ApplicationRecord
    has_many :players, dependent: :destroy_async
  end
Specifically the following part:

    dependent: :destroy_async


Ruby methods can have both positional and keyword arguments, so, in this case, the `has_many` method takes the symbol `:players` as an argument and takes a keyword argument called `dependent:` which is being passed the symbol `:destroy_async`. Where a symbol in Ruby is an immutable identifier.

So a method like this would be defined as something like:

  def has_many(model_name, dependent:)
    puts model_name.to_s
    puts dependent.to_s
  end
Which you could then call as:

  has_many(:players, dependent: :destroy_async)
or

  has_many :players, dependent: :destroy_async
Since you don't need to put parenthesis around method arguments in ruby.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: