In general if you have a "do" you need an "end", although sometimes ruby 
interprets the end of your block for you. (but generally it is good practice to 
always end a "do" with an "end")

Your discrepancy (and confusion) has to do with the way has_many is receiving 
arguments.

In Rails 4, the second parameter is scope (in rails 3, the second parameter was 
options)

> has_many :children, dependent: :destroy, -> { order 'id asc' } do

you have your arity reversed --- the block you are trying to pass in should 
come first, followed by the options (this is documented here 
http://apidock.com/rails/v4.0.2/ActiveRecord/Associations/ClassMethods/has_many)

I don't know why would need or want a "do" statement at the end of that line or 
anywhere in that line -- it is incorrect, just remove it. 




On Aug 18, 2014, at 3:26 PM, Antonio Moreno <[email protected]> wrote:

> Hi,
> 
> in Ruby on Rails 4, let's say a parent has many children. Then I wanted
> to reference only the persisted records in an active record association,
> and followed this link's accepted answer
> (http://stackoverflow.com/questions/19842765/how-do-you-reference-only-the-persisted-records-in-an-active-record-association).
> This works good:
> 
> class Parent < ActiveRecord::Base
>  has_many :children, dependent: :destroy do
>    def persisted
>      collect { |a| a if a.persisted? }
>    end
>  end
> 
> Now, I want to order the associated records:
> 
> has_many :children, dependent: :destroy, -> { order 'id asc' } do
> 
> but this raises an error:
> 
> SyntaxError in ParentsController#index
> 
> ...trunk/app/models/parent.rb:3: syntax error, unexpected
> keyword_do_block, expecting => ...trunk/app/models/parent.rb:49: syntax
> error, unexpected keyword_end, expecting end-of-input
> 
> However, this does work:
> 
> has_many :children, -> { order 'id asc' } do
> 
> I can't even find documentation on how to use the do_block on an
> association. I'd like to know why it doesn't work what I am trying to
> do. Any help appreciated.
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> 
> -- 
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/a5837c82a16e0a31138e8bde53053ded%40ruby-forum.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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/D7250A0E-A002-4759-892D-21809C039263%40datatravels.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to