0

So I have a HABTM relationship between my Students and Classrooms using a join table called ClassroomStudents to handle many students belonging to many classrooms.

A recent business requirement came up that students should be able to request memberships to a classroom, so I thought that I would just create another invitation table containing classroom_id and student_id but quickly realized my classroomstudents table already handled this so I thought about simply re-using this table. I added a boolean accepted field to my classroomstudents table, true representing a membership that has been confirmed and false being the default.

There are multiple ways in which students can be added to classrooms, one of which I want them to be accepted automatically when the relationship is built. So with the following code:

@classroom << @students

How can I also specify a true value for this boolean accepted field instead of the default of false?

Noz
  • 6,216
  • 3
  • 47
  • 82

1 Answers1

1

You could make the default value in the database be true. For example, write a migration that looks like this

class MakeAcceptedDefaultToTrue < ActiveRecord::Migration
  def change
    change_column :classroom_students, :accepted, :boolean, :default => true, :null => false
  end
end
declan
  • 5,605
  • 3
  • 39
  • 43
  • I had considered that as well, but when I'm making a new request to join a classroom would I have to manually create the join table record and set the accepted value to false? (as opposed to being able to use something like `@classroom << @student`) – Noz Oct 18 '12 at 20:12
  • Oh, you're just asking how to make the value be not the default, whatever the default happens to be? Sorry, didn't quite understand your question. – declan Oct 18 '12 at 20:17
  • Yes, you'll have to do it manually. If it's something you find yourself doing in multiple places, you could wrap it in a helper method. – declan Oct 18 '12 at 20:19