[rspec-users] Q: object.id is deprecated. How to mock?

Peter Jaros peter.a.jaros at gmail.com
Tue Nov 25 15:34:15 EST 2008

On Tue, Nov 25, 2008 at 2:04 PM, s.ross <cwdinfo at gmail.com> wrote:
> In Rails, the primary key, by default 'id', is used all over the
> place. However, Ruby now deprecates the use of constructs like:
> @post = Post.find(:first)
> @post_id = @post.id

I think you've got the wrong end of the stick there.  Object#id (which
returns a unique id for the Ruby object) was deprecated in favor of
Object#object_id, mostly because ActiveRecord overrides it to return
the primary key.  #id seemed in retrospect too common to be reserved
for an implementation detail.  You *should* use model.id to get the
primary key.

If you're getting that warning, you're calling #id on something that
doesn't decend from ActiveRecord::Base.  #id is one of the few things
that nil responds to (and issues the deprecation warning); maybe there
are no Posts in your DB?  Try checking the value of @post.


More information about the rspec-users mailing list