0

In Rails 4.0 I have 2 ActiveRecord classes:

class Sequence < ActiveRecord::Base

    has_many :steps, dependent: :destroy

end

and

class Steps < ActiveRecord::Base

    belongs_to :sequence

    default_scope -> { order('order ASC') }

end

When I call mySequence.destroy I get this error:

PG::SyntaxError: ERROR: syntax error at or near "order" LINE 1: ...steps" WHERE "steps"."sequence_id" = $1 ORDER BY order ASC ^ : SELECT "steps".* FROM "steps" WHERE "steps"."sequence_id" = $1 ORDER BY order ASC

When I remove the default scope, the error is gone, but I obviously have to order the steps in my sequence in my code. I did try to define the association like this, and leaving out the default_scope statement:

class Sequence < ActiveRecord::Base

    has_many :steps, dependent: :destroy, order: 'order ASC'

end

but it threw the same error.

Does anyone else have this issue? Is this a bug in Rails? I imagine the ordering is not needed in the destroy sql statement on the children.

GerardV
  • 375
  • 2
  • 11
  • Can you try `mySequence.unscoped.destroy` with the above code you have in your `steps` class – Deej Dec 03 '13 at 22:53
  • Hmm, that produced undefined method `unscoped' for # Do you have a helper method for that? It does hint towards a solution. – GerardV Dec 13 '13 at 20:49

1 Answers1

1

I posted this issue on the rails gitHub issue page as well. gitHub user tanraya suggested the following elegant solution.

default_scope -> { order order: :asc }

Thanks from over here, tanraya, and kudos!

GerardV
  • 375
  • 2
  • 11