This project is archived and is in readonly mode.

#2228 ✓wontfix
Oliver Legg

ActiveRecord::Base#saved?

Reported by Oliver Legg | March 13th, 2009 @ 02:33 PM | in 2.x

A very simple method which is the opposite of ActiveRecord::Base#new_record?

I believe that this is justified (as opposed !new_record?) as it makes my code more readable. For example:

if @record.saved? as opposed to unless @record.new_record?

and also in rspec matchers:

should be_saved as opposed to should_not be_new_record.

Comments and changes to this ticket

  • Oliver Legg

    Oliver Legg March 13th, 2009 @ 02:35 PM

    A very simple method which is the opposite of ActiveRecord::Base#new_record?

    I believe that this is justified (as opposed !new_record?) as it makes my code more readable. For example:

    if @record.saved? as opposed to unless @record.new_record?

    and also in rspec matchers:

    should be_saved as opposed to should_not be_new_record.

  • Jacob Atzen

    Jacob Atzen March 15th, 2009 @ 02:11 PM

    • Tag changed from activerecord to activerecord, feature_request

    Perhaps it would be better to mimic the existing naming scheme, so maybe existing_record? or saved_record? instead of saved? or something to that effect.

  • Jason Gignac

    Jason Gignac March 15th, 2009 @ 05:43 PM

    I second Mr. Atzen -

    saved? could mean "Did my last saved work how I expected it to?" Whereas what you're actually asking is "Is this object already in the database somewhere?" I'd vote for existing_record? for it's obvious parallels to new_record?

    Similarly, I'd change the appended comment from : "Returns true if this object has been saved -- that is, a record for the object exists." to "Returns true if object reflects an existing database record." A test for whether the object ITSELF has been saved would be possibly useful, but not the same thing.

  • Oliver Legg

    Oliver Legg March 16th, 2009 @ 09:58 AM

    I agree with both of those points. I've updated the patch.

  • Rick DeNatale

    Rick DeNatale March 18th, 2009 @ 12:35 PM

    • Tag changed from activerecord, feature_request to activerecord, feature_request, patch

    A minor caveat.

    Under a reasonable interpretation of the semantics under discussion, saved_record? (or whatever the name) is not necessarily the same as !new_record?

    If you save a record in one process, just because you have an instance of the model object you used to save it, doesn't mean that the object STILL exists in the db since it may have been deleted by another process.

    Not sure how important this consideration is.

  • Manfred Stienstra

    Manfred Stienstra March 18th, 2009 @ 12:36 PM

    Can you show me a part of one of your projects where you needed this?

  • Oliver Legg

    Oliver Legg March 18th, 2009 @ 12:58 PM

    @manfred

    Yes. On an edit page for a review. The edit page shares the _form partial with the new action, so we only display the delete button when the review is saved.

    
    <% if @review.saved_record? %>
      <%= button_to "Delete", admin_review_path(@review), {:method => :delete} %>
    <% end %>
    

    While this example could have used !@review.new_record? this patch is all about readability.

  • Eloy Duran

    Eloy Duran March 18th, 2009 @ 01:40 PM

    The problem with saved_record? is that it's ambiguous, it means that the record was already in the database and that it was just saved. Also, like Rick said, it's name doesn't suggest that it's the opposite of new_record?.

    So, I argue that the following is actually more readable:

    
    <% if not @author.new_record? %>
      <%# do stuff %>
    <% end %>
    
  • Rob Anderton

    Rob Anderton March 18th, 2009 @ 01:58 PM

    I'd agree that saved_record? is a bit vague. existing_record? is better but seems like more typing :) I typically use:

    @@@ruby <% unless @author.new_record? %> <%# do stuff %> <% end %>

    
    
    Which I think is clear and concise.
    
  • Rob Anderton

    Rob Anderton March 18th, 2009 @ 01:59 PM

    oops, foiled by the formatting gods...lets try that again:

    
    <% unless @author.new_record? %> 
      <%# do stuff %> 
    <% end %>
    
  • Eloy Duran

    Eloy Duran March 18th, 2009 @ 02:05 PM

    @Rob That's what I would normally do as well. I just used "if not @author.new_record?" to correctly counter argue "if !@author.new_record?".

    Bottom line, Ruby already has the right solution :)

  • Pratik

    Pratik March 18th, 2009 @ 02:59 PM

    Added the first message in the ticket body as it's very weird without it :)

  • RSL
  • Pratik

    Pratik June 21st, 2009 @ 07:46 PM

    • State changed from “new” to “wontfix”

    You can just use record.id instead.

    Thanks.

  • af001

    af001 May 5th, 2011 @ 03:00 AM

    私の中で、総合評価のとっても低いアバアバクロホリスタークロ銀座店。アバクロは大好きなんですけどね。一昨日の東京駅付近での打ち合わせの後、散歩がてら久々に行ってきました。そしたらビックリ!相変わらアバクロず、踊っているだけの店員さんとかもいましたが、

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages