A new model object associated with two parents

Please excuse me if I am asking too many questions. I’ve been
appreciating the help (and I have been learning from reading other
peoples questions and answers too)

Here is a cooked up example of a basic idea I am trying to accomplish

Say that I have a Pupil model . Perhaps also a Teacher, Course models.
Teachers will have things called Course_Offerings. To keep track of a
pupil’s performance in a course there will be a separate model called
Course_performances. These course_performances will belong to both
Pupils and to Course_offerings. In addition to foreign keys, I’d like
the course_performance records to have their own copy of the Pupils
“full_name” for easy access in views.

Given a list of Pupils, is the code below an efficient way of creating a
course_performance instance and associating it with (and assuring that
it gets foreign keys from) two separate models it belongs to?

Do I need to save the new course_performances at all or will they get
saved as I associate them to parent classes?

Will the “transaction” work the way I have it or should I chose the name
of the whole database to apply the transaction to? (I wasn’t clear on
what the book meant me to use)

def create_course_performances
@pupils_to_add = Pupil.find(:all, :conditions => [“some_condition =
“something” ] )
@course_offering =

 @pupils_to_add.each do |current_pupil|
    holder = Course_performance.new(:name => current_pupil.full_name


      Pupil.transaction do
        current_pupil.course_performances << holder
        @course_offering.course_performances << holder


Thanks in advance for any tips.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs