How to setup a one to many relationship?
问题 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: @user.network.title @network.users Thanks 回答1: so network has_many users and a user belongs_to network. Just add a network_id to users table if you still haven't and also since it's a foreign_key is worth indexing it. rails generate migration AddNetworkIdToUsers class AddNetworkIdToUsers < ActiveRecord::Migration def change add_column :users, :network