Skip validation on dependent: :destroy












0















I am having a User and a Company model like below. Company model has some validations.



user.rb



    after_create :create_tables!

def create_tables!
companies.create!(handle: "random_handle")
end


company.rb



before_destroy :check_for_presence_of_a_company!

def check_for_presence_of_a_company!
if user.companies.count <= 1
errors.add(:base, 'You cannot delete all of your companies.')
throw(:abort)
end
end


when user creates account for the first time i create a company for him using after_create in User model and before deleting a company he should have atleast one company.



But the problem is when user tries to delete his account, the above company validations throws error.



When user deletes his account, it should neglect the above validation company.rb. How can i achieve this? Thanks.



Edit



In user.rb i updated



has_many :companies,dependent: :destroy


to



has_many :companies,dependent: :delete_all


But company.rb



has_many :categories, dependent: :destroy


and shows violates foreign key constraint error. Updating it from destroy to delete_all is also not working.










share|improve this question





























    0















    I am having a User and a Company model like below. Company model has some validations.



    user.rb



        after_create :create_tables!

    def create_tables!
    companies.create!(handle: "random_handle")
    end


    company.rb



    before_destroy :check_for_presence_of_a_company!

    def check_for_presence_of_a_company!
    if user.companies.count <= 1
    errors.add(:base, 'You cannot delete all of your companies.')
    throw(:abort)
    end
    end


    when user creates account for the first time i create a company for him using after_create in User model and before deleting a company he should have atleast one company.



    But the problem is when user tries to delete his account, the above company validations throws error.



    When user deletes his account, it should neglect the above validation company.rb. How can i achieve this? Thanks.



    Edit



    In user.rb i updated



    has_many :companies,dependent: :destroy


    to



    has_many :companies,dependent: :delete_all


    But company.rb



    has_many :categories, dependent: :destroy


    and shows violates foreign key constraint error. Updating it from destroy to delete_all is also not working.










    share|improve this question



























      0












      0








      0








      I am having a User and a Company model like below. Company model has some validations.



      user.rb



          after_create :create_tables!

      def create_tables!
      companies.create!(handle: "random_handle")
      end


      company.rb



      before_destroy :check_for_presence_of_a_company!

      def check_for_presence_of_a_company!
      if user.companies.count <= 1
      errors.add(:base, 'You cannot delete all of your companies.')
      throw(:abort)
      end
      end


      when user creates account for the first time i create a company for him using after_create in User model and before deleting a company he should have atleast one company.



      But the problem is when user tries to delete his account, the above company validations throws error.



      When user deletes his account, it should neglect the above validation company.rb. How can i achieve this? Thanks.



      Edit



      In user.rb i updated



      has_many :companies,dependent: :destroy


      to



      has_many :companies,dependent: :delete_all


      But company.rb



      has_many :categories, dependent: :destroy


      and shows violates foreign key constraint error. Updating it from destroy to delete_all is also not working.










      share|improve this question
















      I am having a User and a Company model like below. Company model has some validations.



      user.rb



          after_create :create_tables!

      def create_tables!
      companies.create!(handle: "random_handle")
      end


      company.rb



      before_destroy :check_for_presence_of_a_company!

      def check_for_presence_of_a_company!
      if user.companies.count <= 1
      errors.add(:base, 'You cannot delete all of your companies.')
      throw(:abort)
      end
      end


      when user creates account for the first time i create a company for him using after_create in User model and before deleting a company he should have atleast one company.



      But the problem is when user tries to delete his account, the above company validations throws error.



      When user deletes his account, it should neglect the above validation company.rb. How can i achieve this? Thanks.



      Edit



      In user.rb i updated



      has_many :companies,dependent: :destroy


      to



      has_many :companies,dependent: :delete_all


      But company.rb



      has_many :categories, dependent: :destroy


      and shows violates foreign key constraint error. Updating it from destroy to delete_all is also not working.







      ruby-on-rails ruby activerecord






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 2 '18 at 15:48







      Praveen KJ

















      asked Nov 25 '18 at 17:43









      Praveen KJPraveen KJ

      1231216




      1231216
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Instead of using dependent: :destroy you can add a after_destroy callback and call delete or delete_all (which doesn't run validations).



          # user.rb

          after_destroy :delete_companies

          def delete_companies
          companies.delete_all
          end





          share|improve this answer


























          • Thanks. I have updated the question and still there is dependency problem. Can you answer please?

            – Praveen KJ
            Dec 2 '18 at 15:55



















          1














          I assume you have this (although you didn't show it):



          class User
          has_many :companies, dependent: :destroy
          end


          If so, you can change :destroy to :delete_all:



          has_many :companies, dependent: :delete_all


          Per https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many:




          :delete_all causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).




          (n.b. since callbacks on Company won't be run, if you need additional associations to be deleted, you'd have to handle those separately.)






          share|improve this answer
























          • That solves half the problem. Updating it in question.

            – Praveen KJ
            Dec 2 '18 at 15:44













          • How can i handle those seperately? after_delete is not available. after_destroy isn't working.

            – Praveen KJ
            Dec 2 '18 at 15:53











          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%2f53470173%2fskip-validation-on-dependent-destroy%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Instead of using dependent: :destroy you can add a after_destroy callback and call delete or delete_all (which doesn't run validations).



          # user.rb

          after_destroy :delete_companies

          def delete_companies
          companies.delete_all
          end





          share|improve this answer


























          • Thanks. I have updated the question and still there is dependency problem. Can you answer please?

            – Praveen KJ
            Dec 2 '18 at 15:55
















          1














          Instead of using dependent: :destroy you can add a after_destroy callback and call delete or delete_all (which doesn't run validations).



          # user.rb

          after_destroy :delete_companies

          def delete_companies
          companies.delete_all
          end





          share|improve this answer


























          • Thanks. I have updated the question and still there is dependency problem. Can you answer please?

            – Praveen KJ
            Dec 2 '18 at 15:55














          1












          1








          1







          Instead of using dependent: :destroy you can add a after_destroy callback and call delete or delete_all (which doesn't run validations).



          # user.rb

          after_destroy :delete_companies

          def delete_companies
          companies.delete_all
          end





          share|improve this answer















          Instead of using dependent: :destroy you can add a after_destroy callback and call delete or delete_all (which doesn't run validations).



          # user.rb

          after_destroy :delete_companies

          def delete_companies
          companies.delete_all
          end






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 21:02

























          answered Nov 25 '18 at 19:28









          max pleanermax pleaner

          14.1k42269




          14.1k42269













          • Thanks. I have updated the question and still there is dependency problem. Can you answer please?

            – Praveen KJ
            Dec 2 '18 at 15:55



















          • Thanks. I have updated the question and still there is dependency problem. Can you answer please?

            – Praveen KJ
            Dec 2 '18 at 15:55

















          Thanks. I have updated the question and still there is dependency problem. Can you answer please?

          – Praveen KJ
          Dec 2 '18 at 15:55





          Thanks. I have updated the question and still there is dependency problem. Can you answer please?

          – Praveen KJ
          Dec 2 '18 at 15:55













          1














          I assume you have this (although you didn't show it):



          class User
          has_many :companies, dependent: :destroy
          end


          If so, you can change :destroy to :delete_all:



          has_many :companies, dependent: :delete_all


          Per https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many:




          :delete_all causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).




          (n.b. since callbacks on Company won't be run, if you need additional associations to be deleted, you'd have to handle those separately.)






          share|improve this answer
























          • That solves half the problem. Updating it in question.

            – Praveen KJ
            Dec 2 '18 at 15:44













          • How can i handle those seperately? after_delete is not available. after_destroy isn't working.

            – Praveen KJ
            Dec 2 '18 at 15:53
















          1














          I assume you have this (although you didn't show it):



          class User
          has_many :companies, dependent: :destroy
          end


          If so, you can change :destroy to :delete_all:



          has_many :companies, dependent: :delete_all


          Per https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many:




          :delete_all causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).




          (n.b. since callbacks on Company won't be run, if you need additional associations to be deleted, you'd have to handle those separately.)






          share|improve this answer
























          • That solves half the problem. Updating it in question.

            – Praveen KJ
            Dec 2 '18 at 15:44













          • How can i handle those seperately? after_delete is not available. after_destroy isn't working.

            – Praveen KJ
            Dec 2 '18 at 15:53














          1












          1








          1







          I assume you have this (although you didn't show it):



          class User
          has_many :companies, dependent: :destroy
          end


          If so, you can change :destroy to :delete_all:



          has_many :companies, dependent: :delete_all


          Per https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many:




          :delete_all causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).




          (n.b. since callbacks on Company won't be run, if you need additional associations to be deleted, you'd have to handle those separately.)






          share|improve this answer













          I assume you have this (although you didn't show it):



          class User
          has_many :companies, dependent: :destroy
          end


          If so, you can change :destroy to :delete_all:



          has_many :companies, dependent: :delete_all


          Per https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many:




          :delete_all causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).




          (n.b. since callbacks on Company won't be run, if you need additional associations to be deleted, you'd have to handle those separately.)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 23:29









          showaltbshowaltb

          818410




          818410













          • That solves half the problem. Updating it in question.

            – Praveen KJ
            Dec 2 '18 at 15:44













          • How can i handle those seperately? after_delete is not available. after_destroy isn't working.

            – Praveen KJ
            Dec 2 '18 at 15:53



















          • That solves half the problem. Updating it in question.

            – Praveen KJ
            Dec 2 '18 at 15:44













          • How can i handle those seperately? after_delete is not available. after_destroy isn't working.

            – Praveen KJ
            Dec 2 '18 at 15:53

















          That solves half the problem. Updating it in question.

          – Praveen KJ
          Dec 2 '18 at 15:44







          That solves half the problem. Updating it in question.

          – Praveen KJ
          Dec 2 '18 at 15:44















          How can i handle those seperately? after_delete is not available. after_destroy isn't working.

          – Praveen KJ
          Dec 2 '18 at 15:53





          How can i handle those seperately? after_delete is not available. after_destroy isn't working.

          – Praveen KJ
          Dec 2 '18 at 15:53


















          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%2f53470173%2fskip-validation-on-dependent-destroy%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

          Wiesbaden

          Marschland

          Dieringhausen