Rails has_and_belongs_to_many migration

前端 未结 4 1155
广开言路
广开言路 2020-12-04 05:56

I have two models restaurant and user that I want to perform a has_and_belongs_to_many relationship.

I have already gone into the model fil

4条回答
  •  暖寄归人
    2020-12-04 06:06

    For HABTM relationships, you need to create a join table. There is only join table and that table should not have an id column. Try this migration.

    def self.up
      create_table :restaurants_users, :id => false do |t|
        t.integer :restaurant_id
        t.integer :user_id
      end
    end
    
    def self.down
      drop_table :restaurants_users
    end
    

    You must check this relationship rails guide tutorials

提交回复
热议问题