Join table comment in rails 4 migration

I am new to rails 4 and I'm not quite sure how my join_table should be.

I completed the migration

rails g migration CreateJoinTableQuestionSubTopic question sub_topic

And I get this file

class CreateJoinTableQuestionSubTopic < ActiveRecord::Migration
  def change
    create_join_table :questions, :sub_topics do |t|
      # t.index [:question_id, :sub_topic_id]
      # t.index [:sub_topic_id, :question_id]
    end
  end
end

Why are the two indexes in the comments? I thought that only one of them is the right way?

No need to write

t.column :question_id, :integer
t.column :sub_topic_id, :integer

and / or

t.index :question_id
t.index :sub_topic_id

I would like to have a connection table for performers, but I do not want to do it the old way, if there is a new one on rails 4

thanks for the help

+4
source share
1 answer

The create_join_tablecarry command is new in Rails 4. This is:

create_join_table :questions, :sub_topics do |t|
  # other commands
end

is an abbreviation for this:

create_table :questions_sub_topics do |t|
  t.integer :question_id,  null: false
  t.integer :sub_topic_id, null: false
  # other commands
end

; , . , : Rails , , . sub_topics (question.sub_topics), , question_id, sub_topic_id:

create_join_table :questions, :sub_topics do |t|
  t.index [:question_id, :sub_topic_id]
end

: question_id, .

+9

All Articles