gpt4 book ai didi

ruby-on-rails - 迁移数据库和种子时出现 Ruby 错误

转载 作者:数据小太阳 更新时间:2023-10-29 09:03:16 36 4
gpt4 key购买 nike

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

最佳答案

您不能在迁移中使用 has_manybelongs_to

从问题迁移中删除 has_many,并从答案迁移中删除 belongs_to。你应该没问题。这些方法需要添加到你的模型中,而不是你的迁移中。

关于ruby-on-rails - 迁移数据库和种子时出现 Ruby 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30157858/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com