# \[ANN\] assert\_latest{} detects new model objects

**URL:** <https://www.ruby-forum.com/t/ann-assert-latest-detects-new-model-objects/131394>\
**Category:** Rails\
**Created:** [March 9, 2008, 8:33am UTC](https://www.ruby-forum.com/t/ann-assert-latest-detects-new-model-objects/131394 "2008-03-09T08:33:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [March 9, 2008, 8:33am UTC](https://www.ruby-forum.com/t/ann-assert-latest-detects-new-model-objects/131394/1 "2008-03-09T08:33:38Z")

</div>

Railsers:

This simple test shows assert\_latest{} in action:

```
 c1, c2 = assert_latest Chat do
            Chat.create(:user => aaron, :utterance => 'foo')
            Chat.create(:user => becky, :utterance => 'bar')
          end

 assert{ c1.utterance == 'foo' }
 assert{ c2.utterance == 'bar' }

```

assert\_latest{} sampled the maximum Chat id before yielding to its  
block. Then  
it returned any records found with an id greater than that maximum. This  
helps  
us write more elaborate production code, which might not return its new  
objects  
to its callers.

This blog describes assert\_latest’s rationale and original  
functionality:

[O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/onlamp/blog/2007/07/assert_latest_and_greatest.html)

The latest and greatest version of assert\_latest{} contains some new  
features.

Firstly, the assertion now takes any number of model types. That  
prevents this  
icky situation:

chat = nil  
prop = assert\_latest Prop do  
chat = assert\_latest Chat do  
production\_code\_that\_props\_and\_chats()  
end  
end  
assert{ chat.user == prop.user }

The nested blocks interfered with chat’s scope, forcing un-Ruby-like  
contortions  
when you need to compare the two returned objects. And assert\_latest{}  
is not  
DRY - it appears twice!

To reduce duplication, you may now pass both models into one  
assert\_latest call.  
You can also drop in a diagnostic string:

```
 p, c = assert_latest Prop, Chat, 'diagnostic' do
          Prop.create(:name => 'foo', :price => 42)
          Chat.create(:user => aaron, :utterance => 'bar')
        end

 assert{ p.name == 'foo' }
 assert{ c.utterance == 'bar' }

```

The returned objects can be individual items, or arrays of returned  
items:

```
 p, cz = assert_latest Prop, Chat do
           Prop.create(:name => 'foo', :price => 42)
           Chat.create(:user => aaron, :utterance => 'foo')
           Chat.create(:user => aaron, :utterance => 'bar')
         end

 assert{ p.name == 'foo' }
 assert{ cz.first.utterance == 'foo' }
 assert{ cz. last.utterance == 'bar' }

```

Ruby lets you unroll those returned arrays. This allows you to check  
your  
returned count implicitly:

```
 p, (c1, c2) = assert_latest Prop, Chat do
    ...

```

I added assert\_latest{} (and its evil twin, deny\_latest{}) to the  
assert\_efficient\_sql package, because they both require ActiveRecord.  
Get them  
all, on the usual channels, with Piston:

piston import svn://rubyforge.org/var/svn/efficient-sql/   
vendor/plugins/efficient-sql

Oh, one more feature. When assert\_latest fails, it reflects its called  
block’s  
source into its diagnostic. That requires the RubyReflector module that  
assert{ 2.0 } uses, so you will need that too:

gem install assert2

–  
Phlip  
[http://assert2.rubyforge.org/](http://assert2.rubyforge.org/)
