Having a decrement (-=) function stop at 0 in Ruby












1















I have the following line of code which very simply decrements a Shareholders number of stocks (integer in the DB) when they sell them to someone else. This works well and as expected.



@selling_shareholder.update_attribute(:number_of_stocks, @selling_shareholder.number_of_stocks -= @transaction.number_of_stocks)


Very simply what I'd like to do is have the decrement function stop when the number_of_stocks hits 0 i.e. having a negative number should not be possible.



I guess I could use a simple unless @selling_shareholder.number_of_stocks > 0 at the end of the line, but I'm wondering if that will actually work without using a loop?










share|improve this question


















  • 2





    Riddle me this. How can one have a transaction with a number_of_stocks in excess of their number_of_stocks? This seems like a simple validation validates :number_of_stocks, numericality: {greater_than_or_equal_to: 0} or if transaction is linked to the share holder than validate the number of stocks in the transaction prior

    – engineersmnky
    Nov 14 '18 at 18:16








  • 1





    You are correct, I do not allow shareholders to sell more stocks than they own - and that is controlled in the view already, but what I was looking for here for some more assurance/safety on the back end - which now I've added via your model validation suggestion - thanks! :)

    – JP89
    Nov 14 '18 at 19:59








  • 1





    On a side note, try to avoid using operators like += or -= because they aren't thread-safe

    – Eric Norcross
    Nov 14 '18 at 20:18











  • Thanks, makes sense!

    – JP89
    Nov 15 '18 at 19:17
















1















I have the following line of code which very simply decrements a Shareholders number of stocks (integer in the DB) when they sell them to someone else. This works well and as expected.



@selling_shareholder.update_attribute(:number_of_stocks, @selling_shareholder.number_of_stocks -= @transaction.number_of_stocks)


Very simply what I'd like to do is have the decrement function stop when the number_of_stocks hits 0 i.e. having a negative number should not be possible.



I guess I could use a simple unless @selling_shareholder.number_of_stocks > 0 at the end of the line, but I'm wondering if that will actually work without using a loop?










share|improve this question


















  • 2





    Riddle me this. How can one have a transaction with a number_of_stocks in excess of their number_of_stocks? This seems like a simple validation validates :number_of_stocks, numericality: {greater_than_or_equal_to: 0} or if transaction is linked to the share holder than validate the number of stocks in the transaction prior

    – engineersmnky
    Nov 14 '18 at 18:16








  • 1





    You are correct, I do not allow shareholders to sell more stocks than they own - and that is controlled in the view already, but what I was looking for here for some more assurance/safety on the back end - which now I've added via your model validation suggestion - thanks! :)

    – JP89
    Nov 14 '18 at 19:59








  • 1





    On a side note, try to avoid using operators like += or -= because they aren't thread-safe

    – Eric Norcross
    Nov 14 '18 at 20:18











  • Thanks, makes sense!

    – JP89
    Nov 15 '18 at 19:17














1












1








1








I have the following line of code which very simply decrements a Shareholders number of stocks (integer in the DB) when they sell them to someone else. This works well and as expected.



@selling_shareholder.update_attribute(:number_of_stocks, @selling_shareholder.number_of_stocks -= @transaction.number_of_stocks)


Very simply what I'd like to do is have the decrement function stop when the number_of_stocks hits 0 i.e. having a negative number should not be possible.



I guess I could use a simple unless @selling_shareholder.number_of_stocks > 0 at the end of the line, but I'm wondering if that will actually work without using a loop?










share|improve this question














I have the following line of code which very simply decrements a Shareholders number of stocks (integer in the DB) when they sell them to someone else. This works well and as expected.



@selling_shareholder.update_attribute(:number_of_stocks, @selling_shareholder.number_of_stocks -= @transaction.number_of_stocks)


Very simply what I'd like to do is have the decrement function stop when the number_of_stocks hits 0 i.e. having a negative number should not be possible.



I guess I could use a simple unless @selling_shareholder.number_of_stocks > 0 at the end of the line, but I'm wondering if that will actually work without using a loop?







ruby-on-rails ruby






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 18:09









JP89JP89

10218




10218








  • 2





    Riddle me this. How can one have a transaction with a number_of_stocks in excess of their number_of_stocks? This seems like a simple validation validates :number_of_stocks, numericality: {greater_than_or_equal_to: 0} or if transaction is linked to the share holder than validate the number of stocks in the transaction prior

    – engineersmnky
    Nov 14 '18 at 18:16








  • 1





    You are correct, I do not allow shareholders to sell more stocks than they own - and that is controlled in the view already, but what I was looking for here for some more assurance/safety on the back end - which now I've added via your model validation suggestion - thanks! :)

    – JP89
    Nov 14 '18 at 19:59








  • 1





    On a side note, try to avoid using operators like += or -= because they aren't thread-safe

    – Eric Norcross
    Nov 14 '18 at 20:18











  • Thanks, makes sense!

    – JP89
    Nov 15 '18 at 19:17














  • 2





    Riddle me this. How can one have a transaction with a number_of_stocks in excess of their number_of_stocks? This seems like a simple validation validates :number_of_stocks, numericality: {greater_than_or_equal_to: 0} or if transaction is linked to the share holder than validate the number of stocks in the transaction prior

    – engineersmnky
    Nov 14 '18 at 18:16








  • 1





    You are correct, I do not allow shareholders to sell more stocks than they own - and that is controlled in the view already, but what I was looking for here for some more assurance/safety on the back end - which now I've added via your model validation suggestion - thanks! :)

    – JP89
    Nov 14 '18 at 19:59








  • 1





    On a side note, try to avoid using operators like += or -= because they aren't thread-safe

    – Eric Norcross
    Nov 14 '18 at 20:18











  • Thanks, makes sense!

    – JP89
    Nov 15 '18 at 19:17








2




2





Riddle me this. How can one have a transaction with a number_of_stocks in excess of their number_of_stocks? This seems like a simple validation validates :number_of_stocks, numericality: {greater_than_or_equal_to: 0} or if transaction is linked to the share holder than validate the number of stocks in the transaction prior

– engineersmnky
Nov 14 '18 at 18:16







Riddle me this. How can one have a transaction with a number_of_stocks in excess of their number_of_stocks? This seems like a simple validation validates :number_of_stocks, numericality: {greater_than_or_equal_to: 0} or if transaction is linked to the share holder than validate the number of stocks in the transaction prior

– engineersmnky
Nov 14 '18 at 18:16






1




1





You are correct, I do not allow shareholders to sell more stocks than they own - and that is controlled in the view already, but what I was looking for here for some more assurance/safety on the back end - which now I've added via your model validation suggestion - thanks! :)

– JP89
Nov 14 '18 at 19:59







You are correct, I do not allow shareholders to sell more stocks than they own - and that is controlled in the view already, but what I was looking for here for some more assurance/safety on the back end - which now I've added via your model validation suggestion - thanks! :)

– JP89
Nov 14 '18 at 19:59






1




1





On a side note, try to avoid using operators like += or -= because they aren't thread-safe

– Eric Norcross
Nov 14 '18 at 20:18





On a side note, try to avoid using operators like += or -= because they aren't thread-safe

– Eric Norcross
Nov 14 '18 at 20:18













Thanks, makes sense!

– JP89
Nov 15 '18 at 19:17





Thanks, makes sense!

– JP89
Nov 15 '18 at 19:17












1 Answer
1






active

oldest

votes


















4














I would suggest creating a method in your model called something like #decrement_stock that would handle that logic. You can set all kind of behaviors that should be expected, like raising exceptions. That approach follows the "tell don't ask" principle. It is also a simple unit of work performing a single task, making it easy to test with your favorite unit testing framework.



As engineersmnky suggested, simply adding validation on the model is also a good solution if you want the controller to handle errors.






share|improve this answer
























  • Will go with the validation and logic in the model solution - thanks!

    – JP89
    Nov 14 '18 at 18:41











  • Much better answer. So much so that I'm deleting mine!

    – Gavin Miller
    Nov 15 '18 at 15:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306379%2fhaving-a-decrement-function-stop-at-0-in-ruby%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














I would suggest creating a method in your model called something like #decrement_stock that would handle that logic. You can set all kind of behaviors that should be expected, like raising exceptions. That approach follows the "tell don't ask" principle. It is also a simple unit of work performing a single task, making it easy to test with your favorite unit testing framework.



As engineersmnky suggested, simply adding validation on the model is also a good solution if you want the controller to handle errors.






share|improve this answer
























  • Will go with the validation and logic in the model solution - thanks!

    – JP89
    Nov 14 '18 at 18:41











  • Much better answer. So much so that I'm deleting mine!

    – Gavin Miller
    Nov 15 '18 at 15:46
















4














I would suggest creating a method in your model called something like #decrement_stock that would handle that logic. You can set all kind of behaviors that should be expected, like raising exceptions. That approach follows the "tell don't ask" principle. It is also a simple unit of work performing a single task, making it easy to test with your favorite unit testing framework.



As engineersmnky suggested, simply adding validation on the model is also a good solution if you want the controller to handle errors.






share|improve this answer
























  • Will go with the validation and logic in the model solution - thanks!

    – JP89
    Nov 14 '18 at 18:41











  • Much better answer. So much so that I'm deleting mine!

    – Gavin Miller
    Nov 15 '18 at 15:46














4












4








4







I would suggest creating a method in your model called something like #decrement_stock that would handle that logic. You can set all kind of behaviors that should be expected, like raising exceptions. That approach follows the "tell don't ask" principle. It is also a simple unit of work performing a single task, making it easy to test with your favorite unit testing framework.



As engineersmnky suggested, simply adding validation on the model is also a good solution if you want the controller to handle errors.






share|improve this answer













I would suggest creating a method in your model called something like #decrement_stock that would handle that logic. You can set all kind of behaviors that should be expected, like raising exceptions. That approach follows the "tell don't ask" principle. It is also a simple unit of work performing a single task, making it easy to test with your favorite unit testing framework.



As engineersmnky suggested, simply adding validation on the model is also a good solution if you want the controller to handle errors.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 18:36









Sophie DézielSophie Déziel

404211




404211













  • Will go with the validation and logic in the model solution - thanks!

    – JP89
    Nov 14 '18 at 18:41











  • Much better answer. So much so that I'm deleting mine!

    – Gavin Miller
    Nov 15 '18 at 15:46



















  • Will go with the validation and logic in the model solution - thanks!

    – JP89
    Nov 14 '18 at 18:41











  • Much better answer. So much so that I'm deleting mine!

    – Gavin Miller
    Nov 15 '18 at 15:46

















Will go with the validation and logic in the model solution - thanks!

– JP89
Nov 14 '18 at 18:41





Will go with the validation and logic in the model solution - thanks!

– JP89
Nov 14 '18 at 18:41













Much better answer. So much so that I'm deleting mine!

– Gavin Miller
Nov 15 '18 at 15:46





Much better answer. So much so that I'm deleting mine!

– Gavin Miller
Nov 15 '18 at 15:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306379%2fhaving-a-decrement-function-stop-at-0-in-ruby%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma