Rails未定义方法'has_many'

use*_*193 5 migration rake ruby-on-rails migrate

我正在制作一个数据库:

class CreateUsers < ActiveRecord::Migration
  def change
    has_many :listings, :dependent => :restrict #won't delete if listings exist
    has_many :transactions, :dependent => :restrict #won't del if trans exist
    create_table :users do |t|
      t.integer :key #it's hard to use string as primary
      t.string :identifier_url
      t.string :username
      t.integer :rating

      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

class CreateListings < ActiveRecord::Migration
  def change
    has_one :book
    belongs_to :transaction
    belongs_to :user
    create_table :listings do |t|
      t.integer :key
      t.integer :condition
      t.decimal :price

      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我在任何地方都找不到任何相关内容,所以我猜这是非常基本的东西。

cru*_*uxi 0

您不必在迁移中声明关联,而是在模型中声明关联!