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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:25:14+00:00 2026-05-19T13:25:14+00:00

I got the following models. The join table references the user_id and the course_id

  • 0

I got the following models. The join table references the user_id and the course_id and the combination is set unique. However, after testing my page on the server, e.g. leaving and joining courses many times,also with another current_user my join table created records where the course_id was empty. This got me errors in fetching data afterwards. So now I tried adding :false => null. Could this help? thx for your time

def self.up
  create_table :course_enrollments do |t|
  t.references :user, :null => false
  t.references :course, :null => false
  t.timestamps
end
  add_index :course_enrollments, [:user_id, :course_id], :unique => true
end
  • 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-19T13:25:14+00:00Added an answer on May 19, 2026 at 1:25 pm

    Adding :null => false will cause an exception when you try to save a CourseEnrollment with a null ID. That’s fine if you write the controller action to handle the exception. You could (and should) add validates_presence_of :user_id, :course_id in the CourseEnrollment model, so that instances with null IDs will not be valid, and can be handled with the normal <model>.save method.

    However the bigger question is why does your app save rows with null IDs in the first place? For example, you might be creating orphaned rows (by deleting the associated Course or User). Generally it’s a good idea to add :dependent => :destroy on has_many associations to prevent this.

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

Sidebar

Related Questions

I got following error... System.NullReferenceException: Object reference not set to an instance of an
I've got the following models: An Invoice has several statuses: Initially it's draft, then
I've got the following models: project.rb has_many :tasks task.rb belongs_to :project has_many :assignments has_many
I have got the following models, class City < ActiveRecord::Base belongs_to :state belongs_to :country
In a Rails 2.3.5 application I've got something like the following models: class Foo
I got the following example from http://guides.rubyonrails.org/getting_started.html models/post.rb class Post < ActiveRecord::Base validates :name,
I've got users who are members of groups through a membership join table, and
I've got the following models: In plan.rb has_many :tickets and in ticket.rb belongs_to :plan
I've got the following Restaurant and Comment models. I'm doing full text search on
I've got the following I've pasted into my user model: module ClassMethods Devise::Models.config(self, :email_regexp,

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.