http://pastebin.com/a8fVSCcA 我正在尝试实现一个问题和答案模型,我添加了一个 has_many 和一个 own_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
最佳答案
您不能在迁移中使用 has_many
或 belongs_to
。
从问题迁移中删除 has_many
,并从答案迁移中删除 belongs_to
。你应该没问题..这些方法需要添加到你的模型中,而不是你的迁移中..
关于ruby-on-rails - 迁移数据库和种子时出现 Ruby 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30157858/