anmolagrawal
anmolagrawal

Reputation: 203

How to write foreign keys in many-to-many rich join?

I am trying to create many-to-many rich join between AdminUser and Section with join table as SectionEdit. That is created by generating model. Inside create_section_edits, we write

 create_table :section_edits do |t|
    t.integer :admin_user_id
    t.integer :section_id
    t.timestamps
    t.string :summary
end

Is there any difference in between using :admin_user_id and "admin_user_id"? Same goes for the other primary keys. admin_user_id is foreign key.

Upvotes: 0

Views: 45

Answers (2)

Nicholas.V
Nicholas.V

Reputation: 1936

No, but it is better practice to use the symbol :admin_user_id

As an alternative consider:

create_table :section_edits do |t|
    t.references :admin_user, index: true
    t.references :section, index: true
    t.string :summary
    t.timestamps
end

Notice this way that you can index the foreign keys by adding index: true

Reference: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-references

Upvotes: 1

lalameat
lalameat

Reputation: 754

It should be same in this scenario.

Upvotes: 0

Related Questions