【问题标题】:Rails 3 how to populate a join table with seeds.rbRails 3如何用seeds.rb填充连接表
【发布时间】:2014-12-21 18:24:29
【问题描述】:

该应用程序是 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

学生.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 结尾 结束

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'Reilly)。这本书在一个例子中有这个应用程序,但我看不出他们在哪里或如何播种它。 非常感谢您的关注,非常感谢您的帮助!

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

【问题讨论】:

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


    【解决方案1】:

    当您尝试查找名称为“澳大利亚历史”的课程时,您可能会错过“哪里”。

    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 )
    

    【讨论】:

    • 太棒了!它编译并且工作得很好。现在为了向这个学生传递多个课程,我只是重复这个(即课程:Course.where(name:'foo'),课程:Course.where(name:'bar'))还是有办法将数组或哈希传递给它?
    • 在Where条件下可以传一个数组:courses: Course.where(name: ['foo', 'bar']),会翻译成"IN('foo', 'bar ')" 在 SQL 查询中
    • 你是救生员!!希望其他人能从中受益——谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多