Rails 3如何使用seeds.rb填充连接表

时间:2014-10-26 12:19:44

标签: ruby-on-rails ruby ruby-on-rails-3 mysql2

该应用程序是Rails 3.2.19中教育站点的模型。有课程模型,学生模型和奖励模型。我试图填充一些记录,但我似乎无法填充有课程的用户(预先注册'他们)。我知道有一种方法可以做到这一点,但语法是什么?我已经尝试了我能想到的每一种组合。

这是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

这是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 defnrolled_courses     Course.find(:all) - self.courses   结束 端

... 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

这是我到目前为止所获得的seed.rb文件。我可以分配学生奖励,但不能分配课程。根据架构,我认为我应该使用自动生成的course_id,但我不知道如何引用它来播种一些数据...

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 )

我希望有人可以提供帮助,这是来自一本书(Learning Rails 3 - O&#39; Reilly)。这本书在一个例子中有这个应用程序,但我不知道他们在哪里或如何播种它。 非常感谢您的期待,任何帮助都很受欢迎!

编辑:上面的seeds.rb显示尝试通过课程名称(向第一个学生)申请课程。这是在使用course_id尝试类似方法后完成的。

1 个答案:

答案 0 :(得分:4)

你可能错过&#34;其中&#34;当你试图找到名字为“澳大利亚历史”的课程时,

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 )