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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:28:02+00:00 2026-06-17T21:28:02+00:00

I recently found a problem in a Rails app related to duplicate entries in

  • 0

I recently found a problem in a Rails app related to duplicate entries in a join table. The app is educational, and includes models for Students and Exercises. The join table keeps track of which exercises have been assigned to which students. It doesn’t make sense for an exercise to be assigned to a student more than once (i.e. duplicate entries in the join table shouldn’t be allowed).

I partially fixed the problem by adding a uniqueness validation to the join table (see the last line of code, below). This validation will prevent any new duplicate entries from being created in the future. I’m still faced with the problem, however, of addressing the existing duplicates in the table.

Is there a way to run the new validation against the entire existing database, to retrieve all the records that are no longer valid?

class Student < ActiveRecord::Base
  has_many :student_exercise_assignments
  has_many :exercises, :through => :student_exercise_assignments
end

class Exercise < ActiveRecord::Base
  has_many :student_exercise_assignments
  has_many :students, :through => :student_exercise_assignments
end

class StudentExerciseAssignment < ActiveRecord::Base
    belongs_to :student
    belongs_to :exercise
    validates :exercise_id, :uniqueness => { :scope => :student_id, :message => "An exercise can only be assigned to a student once" }

UPDATE

Shioyama’s answer below gives exactly the information I was looking for. Being a Rails newbie, though, I was a bit confused by his use of & in &:invalid?. If anyone needs an primer of the & operator in Ruby, there’s a good one here.

  • 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-06-17T21:28:04+00:00Added an answer on June 17, 2026 at 9:28 pm

    How about just:

    invalid_assignments = StudentExerciseAssignment.select(&:invalid?)
    

    This will select all assignments which return true when called with the invalid? method. You can then do whatever you need to do to fix them.

    As a side note, I would also suggest adding a uniqueness constraint to your database. See this answer for reasons why: Rails: Validation in model vs migration

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

Sidebar

Related Questions

I use cookies to manage user sessions in my Rails app. I recently found
This is a very strange problem. I've got a rails app in which I
I recently found a problem, which can add to technology of calender A user
I recently found a situation in which I had two related variables that had
I recently came across this problem and found a solution but I'm wondering if
I've been stuck on a problem recently for a little while and found my
I recently found a problem in my Compile Sources Build Phase, it was compiling
I recently found out about the Paperclip image uploading functionality for Ruby on Rails.
I am using threads in a python program and recently found a problem where
I recently found a contest problem that asks you to compute the minimum number

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.