views:

73

answers:

2

I am experiencing a weird bug after adding this to my Alternative-model:

default_scope order(:number)

On the first page refresh, everything works fine. On subsequent refreshes, I get

NoMethodError in Pages#show 
undefined method `written?' for #<Alternative:0x10455c558>

Alternative most assuredly has the written? method. I am running Passenger, so after a

touch tmp/restart.txt

the page works again. For the first refresh. But the error message is back on subsequent refreshes again. Removing the default_scope solves the issue (but only after another server restart).

Edit: The bug is also present when running on WEBrick through rails server, so it is not connected to Passenger.

Edit: written? is an instance method on Alternative. The bug also appears when trying to access methods gained through has_many, like this:

has_many :restrictions

alternative.restrictions.empty? # => NoMethodError

Anyone else experiencing problems with default_scope in Rails 3? Or have a solution? Thanks!

+1  A: 

I have not experienced odd problems with default_scope, but I did find that Passenger wasn't reloading code properly in development mode with the current Rails 3 HEAD -- leading to baffling assertions like the one you ran into.

Does it work if you just run "rails server"?

Nick Ragaz
Good suggestion. Unfortunately the bug rears its head also when running on WEBrick through `rails server`
Magnar
If `written` is an attribute on Alternative, is it possible to identify the SQL that is loading the Alternative model that is receiving `written?`? Could it be that it is somehow not loading the `written` column? I realize the inconsistency would still make no sense, but it does look similar to what happens if you did `Alternative.select` and didn't specify the `written` column.
Nick Ragaz
No, `written?` is a method on `Alternative` and not directly tied to the db-model. Also, any other method call has the same buggy behaviour.
Magnar