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

s.ross cwdinfo at gmail.com
Tue Nov 25 17:34:09 EST 2008

On Nov 25, 2008, at 12:34 PM, Peter Jaros wrote:

> 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.
> Peter

Thanks Peter, Nick, Pat and Mark. I think I have a better handle on  
this now. I'm up-migrating what is appearing to me to be an  
inconsistent codebase to Rails 2.2 and the issue of indexing versus  
member access came up. It appears I was incorrect in my assessment  
that using a hash index would be the safer way to go. Thankfully we  
have a reasonable number of specs and they will help us find and fix a  
many of these issues.


More information about the rspec-users mailing list