Always have to run views twice to get updated data

To give you a speedier response, view queries run against “stale” indexes by default. Couchbase then re-runs the view to refresh the index after it gives you the response.

irb(main):008:0> TestDoc.by_user_id.to_a.count
=> 5

At this point, Couchbase runs the view again to update the index. The index still shows 5.

irb(main):009:0> TestDoc.create(id: "testing123")
=> #<TestDoc id: testing123, created_at: 2015-03-23 09:14:47 -0400, type: "txn", updated_at: 2015-03-23 09:14:47 -0400>

Couchbase does not re-run the view on insertion.

### I can wait 5 minutes here and it does not make a difference 

You can configure Couchbase to automatically update production indexes. Auto updates don’t work on development views, though.

irb(main):010:0> TestDoc.by_user_id.to_a.count
=> 5

At this point, Couchbase now re-runs the view to rebuild the index … and your new document is now in the index as you see when you re-run your query below.

irb(main):011:0> TestDoc.by_user_id.to_a.count
=> 6

You can get around this by specifying stale=false in your query. That’ll make Couchbase re-run the view before it returns the result, and of course there’ll be a performance impact.

Cheers!