B. Bulpett
B. Bulpett

Reputation: 823

Rails 3 how to populate a join table with seeds.rb

The application is a mockup of an educational site in Rails 3.2.19. There is a Courses model, a Students model, and an Awards model. I am trying to populate with a few records, but I can't seem to populate the users having courses ('pre-registering' them). I know there's a way to do this, but what is the syntax? I've tried every combination I can think of.

Here is the schema.rb

ActiveRecord::Schema.define(:version => 20141021184426) do

  create_table "awards", :force => true do |t|
    t.string   "name"
    t.integer  "year"
    t.integer  "student_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "courses", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "courses_students", :id => false, :force => true do |t|
    t.integer "course_id",  :null => false
    t.integer "student_id", :null => false
  end

 add_index "courses_students", ["course_id", "student_id"],
 :name =>"index_courses_students_on_course_id_and_student_id", :unique => true

   create_table "students", :force => true do |t|
    t.string   "given_name"
    t.string   "middle_name"
    t.string   "family_name"
    t.date     "date_of_birth"
    t.decimal  "grade_point_average", :precision => 10, :scale => 0
    t.date     "start_date"
    t.datetime "created_at",                                         :null => false
    t.datetime "updated_at",                                         :null => false
  end

end

this is course.rb...

class Course < ActiveRecord::Base
  attr_accessible :name, :student_id, :course_id
  has_and_belongs_to_many :students
end

student.rb...

class Student < ActiveRecord::Base

  attr_accessible :date_of_birth, :family_name, :given_name, :grade_point_average,
  :middle_name, :start_date, :student_id, :courses

  has_many :awards, :dependent => :destroy

  has_and_belongs_to_many :courses

  def name
    given_name + " " + family_name
  end

  def enrolled_in?(course)
    self.courses.include?(course)
  end

def unenrolled_courses Course.find(:all) - self.courses end end

award.rb...

class Award < ActiveRecord::Base
  attr_accessible :name, :student_id, :year

  # every award is linked to a student, through student_id
  belongs_to :student
  validates_existence_of :student, :both => false
end

This is the seed.rb file I've got so far. I can assign the student awards, but not courses. According to the schema, I think I'm supposed to use the auto-generated course_id but I don't know how to reference that to seed some data...

Course.create(name: 'Australian History')
Course.create(name: 'Advanced Java Programming')
Course.create(name: 'Introduction to Rails')
Course.create(name: 'Psychology')

Student.create(given_name: 'Donna', middle_name: 'Louise', family_name:'Chang',
  date_of_birth:'1981-11-04', grade_point_average: 3.55, start_date: '2014-01-13',
  courses: Course(:name=> 'Australian History'))

Student.create(given_name: 'Barnabas', middle_name: 'Justin', family_name:'Bulpett',
  date_of_birth: '1966-07-28', grade_point_average: 2.83, start_date: '2012-08-15')

Student.create(given_name: 'Cleveland', middle_name: 'Jackson', family_name:'Brown',
  date_of_birth: '1976-02-18', grade_point_average: 3.96, start_date: '2011-08-18')

Award.create(name: 'Heisman Trophy', year: 2012, student_id: 3 )
Award.create(name: 'Nobel Peace Prize', year: 2012, student_id: 1 )
Award.create(name: 'Best Cinematography', year: 2012, student_id: 2 )

I hope someone can help, this is from a book (Learning Rails 3 - O'Reilly). The book has this application in an example, but I can't see where or how they seeded it. Thank you so much for looking, any help is appreciated alot!

Edit: the above seeds.rb shows an attempt to apply a course via the course name (to the first student). This was done after trying a similar approach using course_id.

Upvotes: 2

Views: 2752

Answers (1)

Jan Strn&#225;dek
Jan Strn&#225;dek

Reputation: 808

You're probably missing "where" when you're trying to find a courses with name 'Australian History'.

Course.create(name: 'Australian History')
Course.create(name: 'Advanced Java Programming')
Course.create(name: 'Introduction to Rails')
Course.create(name: 'Psychology')

Student.create(given_name: 'Donna', middle_name: 'Louise', family_name:'Chang', date_of_birth:'1981-11-04', grade_point_average: 3.55, start_date: '2014-01-13', courses: Course.where(name: 'Australian History'))

Student.create(given_name: 'Barnabas', middle_name: 'Justin', family_name:'Bulpett', date_of_birth: '1966-07-28', grade_point_average: 2.83, start_date: '2012-08-15')

Student.create(given_name: 'Cleveland', middle_name: 'Jackson', family_name:'Brown', date_of_birth: '1976-02-18', grade_point_average: 3.96, start_date: '2011-08-18')

Award.create(name: 'Heisman Trophy', year: 2012, student_id: 3 )
Award.create(name: 'Nobel Peace Prize', year: 2012, student_id: 1 )
Award.create(name: 'Best Cinematography', year: 2012, student_id: 2 )

Upvotes: 4

Related Questions