This project is archived and is in readonly mode.
ActiveRecord::Base#saved?
-
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 tounless @record.new_record?and also in rspec matchers:
should be_savedas opposed toshould_not be_new_record. -
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.
-
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.
-
Oliver Legg
I agree with both of those points. I've updated the patch.
-
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.
-
Manfred Stienstra
Can you show me a part of one of your projects where you needed this?
-
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.
-
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 %> -
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. -
Rob Anderton
oops, foiled by the formatting gods...lets try that again:
<% unless @author.new_record? %> <%# do stuff %> <% end %> -
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 :)
-
Pratik
Added the first message in the ticket body as it's very weird without it :)
-
RSL
Does this cross use with my patch here: http://rails.lighthouseapp.com/p...
