http://pastebin.com/a8fVSCcA 我正在尝试实现一个问题和答案模型,我添加了一个has_many和一个belongs_to但它们似乎导致了错误。
reconcubed@blccit:~/workspace (master) $ rake db:migrate
== 20150509125146 CreateQuestions: migrating ==================================
-- create_table(:questions)
-- has_many(:answers)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `has_many' for #<CreateQuestions:0x00000006b93010>/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.1.6/lib/active_record/migration.rb:648:in `block in method_missing'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.1.6/lib/active_record/migration.rb:621:in `block in say_with_time'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.1.6/lib/active_record/migration.rb:621:in `say_with_time'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.1.6/lib/active_record/migration.rb:641:in `method_missing'
/home/ubuntu/workspace/db/migrate/20150509125146_create_questions.rb:7:in `block in change'
有问题的代码:
#Questions Model
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.string :title
t.text :body
t.boolean :resolved
has_many :answers
t.timestamps
end
end
end
#Answers
class CreateAnswers < ActiveRecord::Migration
def change
create_table :answers do |t|
t.text :body
t.references :question, index: true
belongs_to :question
t.timestamps
end
end
end
答案 0 :(得分:2)
您无法在迁移中使用has_many
或belongs_to
。
从问题迁移中删除has_many
,然后从答案迁移中删除belongs_to
。你应该没事。这些方法需要添加到你的模型中,而不是你的迁移..