Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 937735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:31:32+00:00 2026-05-15T21:31:32+00:00

I’ve found myself starting to leverage checking persistence to get my models to ‘work’.

  • 0

I’ve found myself starting to leverage checking persistence to get my models to ‘work’. It seems convenient and correct to include a persistence check. On the other hand, it feels a little shady, as if I was being overly cautious or breaking the ORM abstraction in a small way.

An example might be:

class Shipment
  include DataMapper:Resource

  belongs_to :address, :required => false

  def shippable?
    valid? && persisted? && !address.nil? && address.valid? && address.persisted?
  end
end

In this case, I need to have a method telling me if a shipment is shippable. This is true when it is valid, saved to the db, and has an address saved.

Another example might be using it in callbacks to determine whether certain things (price recalculation) need to happen.

Is this code safe and correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:31:32+00:00Added an answer on May 15, 2026 at 9:31 pm

    Well, if it is really possible that you come to the point of performing the shipment without a confirmed persistance or a reload from the database, checking persistance seems absolutely necessary to me.

    But is this really the case? How do you get to this shipping action? I would think of two workflows here:

    1. There is an order which you want to ship. You click on “ship this shit!” and get to ShipmentsController#new, enter your data. By pressing “save” your shipment gets validated and persisted if valid. After the persisting is successful (I assume shipment.save returns false if not) you can directly move on with your shipping action (whatever needs to be done there). If shipment.save tells you it did not persist, the #new view gets rendered another time and shipping is not performed yet. So this is the idea of wizard-like workflow with a “gate” that only lets you perform the shipping if persistance has been successful.

    2. The creation of shipments and shipment of shipments is completely decoupled. Lets say one guy plans shipments and another one performs them. The former creates a new, valid, persisted shipment. The latter begins with a list of readily planned shipments. This ShipmentsController#index loads the shipments directly from the database using Shipment.all. No need to check persisted? then in the ShipmentsController#perform action.

    Ok. let’s make it more complicated:
    Assume now we are in scenario 2 and the first guy could delete the shipment while the second one performs it. Without regarding this case, the naive application would have saved the shipment again after #perform has finished, setting the date of shipment. This would mean that the lost “delete” database anomaly has happened. However, won’t you use a state machine anyway? So lets say there are the transistions planned -> deleted and planned -> shipped. If guy #1 sets the status to deleted while guy #2 is performing the shipment, the state machine would throw an exception: “No transition from deleted to shpped, my dear!” when trying to update the status.

    So I can’t really make up a scenario where checking persistence explicitly is not paranoid. But if you have one where it could really occur due to a ‘unsafe’ workflow, you should check for persistance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 482k
  • Answers 482k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can have two columns of type timestamp in one… May 16, 2026 at 6:39 am
  • Editorial Team
    Editorial Team added an answer Ok so of course I find the answer right after… May 16, 2026 at 6:39 am
  • Editorial Team
    Editorial Team added an answer In general, no. Unless your lines are fixed length or… May 16, 2026 at 6:39 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.