简体   繁体   中英

Recommended way to create a joint table using ruby on rails: rails generate scaffold, rails generate model, or rails generate migration?

I want to create a third table to indicated the many-to-many relation between table A and table B. Say table A corresponds to a Course model and table B correspond to a Subject table. Which of the following way do you recommend to generate the third joint table:

  1. rails generate scaffold Course_subject course_id:integer subject_id:integer
  2. rails generate model course_subject
  3. rails generate migration course_subject understanding

My understanding is that using option 1 will generate the controller, model and view; option 2 doesn't generate the controller so if I want to see the result on website I will need to create the view file by hand; options 3 simply generates a migration file, nothing else. Am I getting this correctly? And is option 1 more convenient or not necessary?

Yes, you are doing it correctly. They are all correct as long as it serves its purpose. It boils down to what you want to accomplish.

Option 1 will give you everything you need but that will give you unnecessary views . In rails, you still have control over your files/code. Removing a file is just a click away. Scaffolding is just a tool to help us develop apps with less effort.

If it is a basic association where you only have foreign keys, then you can use option 2. Generate a model with a migration. You can generate controller later when you need it.

Option 3 will generate a migration but will not include the CourseSubject model. You have to create it yourself. Course and Subject model should define has_many associations and CourseSubject model with belongs_to association. This will make course.subjects chaining work.

class CourseSubject
  belongs_to :course
  belongs_to :subject
end

Command 1 creates a resource from scratch: migration, model, routing, controller and views. It also creates tests and some assets:

$ rails g scaffold course_subject course_id:integer subject_id:integer
  invoke  active_record
  create    db/migrate/20181022020611_create_course_subjects.rb
  create    app/models/course_subject.rb
  invoke    test_unit
  create      test/models/course_subject_test.rb
  create      test/fixtures/course_subjects.yml
  invoke  resource_route
   route    resources :course_subjects
  invoke  scaffold_controller
  create    app/controllers/course_subjects_controller.rb
  invoke    erb
  create      app/views/course_subjects
  create      app/views/course_subjects/index.html.erb
  create      app/views/course_subjects/edit.html.erb
  create      app/views/course_subjects/show.html.erb
  create      app/views/course_subjects/new.html.erb
  create      app/views/course_subjects/_form.html.erb
  invoke    test_unit
  create      test/controllers/course_subjects_controller_test.rb
  create      test/system/course_subjects_test.rb
  invoke    helper
  create      app/helpers/course_subjects_helper.rb
  invoke      test_unit
  invoke    jbuilder
  create      app/views/course_subjects/index.json.jbuilder
  create      app/views/course_subjects/show.json.jbuilder
  create      app/views/course_subjects/_course_subject.json.jbuilder
  invoke  assets
  invoke    coffee
  create      app/assets/javascripts/course_subjects.coffee
  invoke    scss
  create      app/assets/stylesheets/course_subjects.scss
  invoke  scss
  create    app/assets/stylesheets/scaffolds.scss

Command 2 creates migration, model and test for model:

$ rails g model course_subject
  invoke  active_record
  create    db/migrate/20181022020655_create_course_subjects.rb
  create    app/models/course_subject.rb
  invoke    test_unit
  create      test/models/course_subject_test.rb
  create      test/fixtures/course_subjects.yml

Command 3 creates migration only:

$ rails g migration course_subject
  invoke  active_record
  create    db/migrate/20181022020717_course_subject.rb

Which option to choose depends on association you're going to use. For plain has_and_belongs_to_many association you do not need a model for a join table. Just create a migration then - command 3 is your choice. Note that the join table should follow some naming rules and in this case be named courses_subjects . Example of migration:

create_table courses_subjects, id: false do |t|
  t.references  :course
  t.references  :subject
end

(Fields id , created_at and updated_at are not created since they are not necessary here)

Otherwise, if for some reason you're going to have access to join table using model class, or for has_many ... through association, you will need to create a model as well. So use command 2 then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM