What does the model association option -> mean? order matters?

Currently I have this and it works:

has_many :reports, -> { order(id: :desc) }

I added this and it works:

has_many :reports, -> { order(id: :desc) }, :dependent => :destroy

But this doesnt:

has_many :reports, :dependent => :destroy, -> { order(id: :desc) }

Can someone explain why the last case fails?

What does -> do anyhow?

Thanks.

Currently I have this and it works:

has_many :reports, -> { order(id: :desc) }

I added this and it works:

has_many :reports, -> { order(id: :desc) }, :dependent => :destroy

But this doesnt:

has_many :reports, :dependent => :destroy, -> { order(id: :desc) }

Can someone explain why the last case fails?

Most likely to do with the order of the arguments. The second argument is expected to be a scope, or it can be nil and then it passes to the options hash.

What does -> do anyhow?

-> means create a Proc and pass in the current object. You might try order: { id: :desc } and see if that works. I think you're getting the arguments too deeply nested here.

Also read the docs for has_many at API Dock. Also you may be missing an opportunity to use a scope for your ordering, since the second argument to has_may is supposed to be a scope.

Walter