Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Rails的敏捷Web开发-ActiveRecord::StatementInvalid错误_Ruby On Rails - Fatal编程技术网

Ruby on rails 使用Rails的敏捷Web开发-ActiveRecord::StatementInvalid错误

Ruby on rails 使用Rails的敏捷Web开发-ActiveRecord::StatementInvalid错误,ruby-on-rails,Ruby On Rails,我是Rails新手,已经开始学习“Rails的敏捷Web开发:第四版”。我目前在购物车创建章节。我主要是从书中复制代码,但是我的数据库被称为“DVD”,而不是“产品”(因为我正在为一个项目创建DVD商店) 我已经到了本章的末尾,当我运行功能测试时,我得到以下错误: test_should_destroy_dvd(DvdsControllerTest): ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: l

我是Rails新手,已经开始学习“Rails的敏捷Web开发:第四版”。我目前在购物车创建章节。我主要是从书中复制代码,但是我的数据库被称为“DVD”,而不是“产品”(因为我正在为一个项目创建DVD商店)

我已经到了本章的末尾,当我运行功能测试时,我得到以下错误:

test_should_destroy_dvd(DvdsControllerTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: line_items.dvd_id: SELECT COUNT(*) FROM "line_items" WHERE ("line_items".dvd_id = 980190962)
app/models/dvd.rb:26:in `ensure_not_referenced_by_any_line_item'
app/controllers/dvds_controller.rb:76:in `destroy'
test/functional/dvds_controller_test.rb:50:in `test_should_destroy_dvd'
test/functional/dvds_controller_test.rb:49:in `test_should_destroy_dvd'
下面是它引用的dvd.rb文件中的代码:

has_many :line_items 
before_destroy :ensure_not_referenced_by_any_line_item
...
private
  def ensure_not_referenced_by_any_line_item
if line_items.empty?
  return true
else
  errors.add(:base, 'Line Items present')
  return false
 end
end
DVD_controller.rb中的代码:

def destroy
    @dvd = Dvd.find(params[:id])
    @dvd.destroy

    respond_to do |format|
      format.html { redirect_to(dvds_url) }
      format.xml  { head :ok }
    end
  end
以及DVD_controller_test.rb中的代码:

test "should destroy dvd" do
    assert_difference('Dvd.count', -1) do
      delete :destroy, :id => @dvd.to_param
    end

    assert_redirected_to dvds_path
  end
基本上,我不理解这个错误,我正在寻求帮助,让它消失!我猜问题在于“没有这样的专栏:line\u items.dvd\u id”,但我不知道如何修改它。我已经融化了我的大脑试图找出它,所以任何帮助将不胜感激

编辑: 这似乎就是问题所在,在create_line_items.rb中:

class CreateLineItems < ActiveRecord::Migration
  def self.up
    create_table :line_items do |t|
      t.integer :product_id
      t.integer :cart_id

      t.timestamps
    end
  end

  def self.down
    drop_table :line_items
  end
end 

确保运行rake db:migrate来创建数据库。

就像Cat所说的,确保运行了迁移,但还要注意,您可能忘记了在迁移中更改列名。我的猜测是,在过去,它是产品id。如果您还发布了与LineItem和当前db/schema.rb相关的迁移,这将非常有用。

架构看起来不错,我认为,迁移缺少dvd id。您的架构中是如何得到dvd id的?您是否有另一个添加它的迁移

可能值得检查数据库中的实际内容:

sqlite3 db/development.sqlite3

sqlite3> .schema line_items

检查行项目是否确实有dvd\u id列

我刚刚仔细检查了它,并运行了迁移。谢谢你的钱:product_id。我用上面的迁移和模式代码编辑了我的原始帖子。我怎样才能解决这个问题?多谢各位!啊,我试着给你的专栏重新命名,看看它能不能解决这个问题。(为了清晰起见,我已经将上面的代码改回原来的形式)是的,你是对的,我的问题是我有一个product_id列,而不是dvd_id列。如何修复此问题?您需要创建另一个具有rename\u列的迁移。然后重新运行rakedb:migrate
sqlite3 db/development.sqlite3

sqlite3> .schema line_items