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 5956327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:14:27+00:00 2026-05-22T18:14:27+00:00

I have a teachers model that accepts_nested_attributes_for klasses. I don’t want to be able

  • 0

I have a teachers model that accepts_nested_attributes_for klasses. I don’t want to be able to delete the klass if there are users in the klass and I want to print a validation message.

This is what I have…

class Teacher < ActiveRecord::Base
    accepts_nested_attributes_for :klasses, :reject_if => proc { |a| a['name'].blank? }, :allow_destroy => true
end

class Klass < ActiveRecord::Base

    belongs_to :teacher
    has_many :users
    before_destroy :reject_if_has_users

    private 

    def reject_if_has_users
      if users.length > 0
        errors.add_to_base "You cannot delete this class as it contains users."
        false
      else
        true
      end
    end

end

I want the validation to fail when I try removing a klass from a teacher that contains users when I am using accepts_nested_attributes_for and passing in a “klasses_attributes”=>{“0″=>{“name”=>”test”, “_destroy”=>”1”, “id”=>”17183”} hash.

The above method will prevent the klass from being removed if I don’t have the error.add_to_base line. It won’t however cause a validation message to be shown. When I have that line, it doesn’t prevent the deletion.

Thanks,

Tim

  • 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-22T18:14:28+00:00Added an answer on May 22, 2026 at 6:14 pm

    Validation doesn’t take place when deleting a record, only when saving a record. The way I handled this in one of my projects is raising an exception in the model and then rescueing from that exception in my controller and showing a flash alert. Like this:

    Model:

    class Klass < ActiveRecord::Base
      before_destroy :reject_if_has_users
    
      private 
    
      def reject_if_has_users
        raise "You cannot delete this class as it contains users." if users.length > 0
      end
    end
    

    Teachers controller:

    def update
      # Code for updating the teacher
    rescue RuntimeError => e
      redirect_to teacher_url(@teacher, :alert => e.message) and return false
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using joomla 1.7 and I want for some users to not have the
I have the following models: Students, Teachers, Administrator What I want to do is
I have a users controller, where if I add a user I want to
I have two models. User and Teacher model. The teacher model should be a
I have 2 models - Teacher and Subject . A want to connect them
I have two tables : teachers (teacher_id,teacher_name) courses (teacher_id,course_id) And I need to display
We have studied the Variable Elimination recently and the teacher emphasizes that it is
I have a simple entity data model where I have two entities and a
I have to implement an algorithm that generates a timetable for a university. I've
I have a teacher model which has_many students. When I render a student (as

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.