Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord::Base#saved?

#2228

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.

Reported by Oliver Legg · March 13th, 2009 @ 02:33 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Oliver Legg
    Oliver Legg

    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.

    March 13th, 2009 @ 02:35 PM

  2. Jacob Atzen
    Jacob Atzen
    • 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.

    March 15th, 2009 @ 02:11 PM

  3. Jason Gignac
    Jason Gignac

    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.

    March 15th, 2009 @ 05:43 PM

  4. Oliver Legg
    Oliver Legg

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

    March 16th, 2009 @ 09:58 AM

  5. Rick DeNatale
    Rick DeNatale
    • 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.

    March 18th, 2009 @ 12:35 PM

  6. Manfred Stienstra
    Manfred Stienstra

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

    March 18th, 2009 @ 12:36 PM

  7. Oliver Legg
    Oliver Legg

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

    March 18th, 2009 @ 12:58 PM

  8. Eloy Duran
    Eloy Duran

    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 %>
    

    March 18th, 2009 @ 01:40 PM

  9. Rob Anderton
    Rob Anderton

    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.
    

    March 18th, 2009 @ 01:58 PM

  10. Rob Anderton
    Rob Anderton

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

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

    March 18th, 2009 @ 01:59 PM

  11. Eloy Duran
    Eloy Duran

    @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 :)

    March 18th, 2009 @ 02:05 PM

  12. Pratik
    Pratik

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

    March 18th, 2009 @ 02:59 PM

  13. RSL
  14. Pratik
    Pratik
    • State changed from new to wontfix

    You can just use record.id instead.

    Thanks.

    June 21st, 2009 @ 07:46 PM

  15. af001
    af001

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

    May 5th, 2011 @ 03:00 AM