2012-11-24 82 views
3

我正在做一个数据库:轨道未定义的方法“的has_many”

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 

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 

我不能在这个地方找到任何东西,所以我猜测它的东西很基本的。

回答

0

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

2

关联(has_many,belongs_to等)应该在模型中声明,而不是在迁移中。

这是一个良好的阅读开始与迁移: http://guides.rubyonrails.org/migrations.html

而这其中的关联: http://guides.rubyonrails.org/association_basics.html

+0

但对于使用“belongs_to的:用户“,在表格中必须有一列user_id?!? – Klaus

+0

@克劳斯,当然可以。如果您使用生成器来创建该列的模型已经在您那里 – TopperH

0

把你的关联模型

class Member < ActiveRecord::Base 

has_many :listings, :dependent => :restrict 
has_many :transactions, :dependent => :restrict 

end 
相关问题