Ruby on Rails Friday, July 22, 2016

> On Jul 21, 2016, at 5:05 PM, S Ahmed <sahmed1020@gmail.com> wrote:
>
> 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

>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAG2rwuNK4aF1bvPXMTe-vxMr7u8jN-5z2Z76GBVin_RUk6SmAQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/80142F09-DC7A-42F5-9F16-F95D35368ADD%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment