Rails 3: belongs_to, has_one and Migrations

前端 未结 3 1704
一向
一向 2021-01-18 12:36

I\'m new to Rails, and I\'m coming to it from a Django background. I\'ve come to terms with the fact that models and the database schema are separate in Rails, online Django

相关标签:
3条回答
  • 2021-01-18 12:53

    You have to add the foreign key in your migration file, something like this:

    def change
      create_table :songs do |t|
        t.references :artist
      end
    
      add_index :songs, :artist_id
    end
    
    0 讨论(0)
  • 2021-01-18 12:58

    Now, in Rails 4 you can do:

    class AddProcedureIdToUser < ActiveRecord::Migration
      def change
        add_reference :users, :procedure, index: true
      end
    end
    

    to an existing Model

    0 讨论(0)
  • 2021-01-18 13:10

    You can generate migration

    rails g migration AddProcedureIdToUser procedure:references 
    

    Thanks

    0 讨论(0)
提交回复
热议问题