How to setup a one to many relationship?

后端 未结 3 1362
轻奢々
轻奢々 2021-02-05 05:36

I have the following models:

User (id, name, network_id)
Network(id, title)

What kind of Rails model assoc do I need to add so that I can do:

3条回答
  •  生来不讨喜
    2021-02-05 05:53

    This is my way: run:

    $rails generate migration AddNetworkIdToUsers
    

    then config migration file:

    class AddNetworkIdToUsers < ActiveRecord::Migration[5.1]
    
      def up
    
        add_column :users, :network_id, :integer
        add_index  :users, :network_id
      end
    
      def down
    
        remove_index :users, :network_id
        remove_column :users, :network_id
      end
    
    end
    

提交回复
热议问题