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.

Reply via email to