2011-11-25 3 views
0

레일스에 익숙하고 레일즈 애자일 웹 개발 : 제 4 판을 통해 작업을 시작했습니다. 현재 Cart Creation 장에 있습니다. 나는 대부분 책에서 코드를 복사했지만, 데이터베이스는 '제품'대신 'dvds'라고 불린다. 왜냐하면 프로젝트 용 DVD 스토어를 만들기 때문이다. 여기를 참조 년대 dvd.rb 파일에서 코드의레일즈가있는 애자일 웹 개발 - ActiveRecord :: StatementInvalid Error

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' 

:

나는 장의 마지막에 도달했습니다 내가 기능 테스트를 실행할 때, 나는 다음과 같은 오류를 받고 있어요

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 

dvds_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 

그리고 dvds_controller_test.r의 코드 b :

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_items.dvd_id"라고 생각하지만 수정 방법을 모르겠습니다. 나는 뇌를 녹여서 알아 내려고 노력했기 때문에 어떤 도움이든지 크게 감사 할 것입니다.

편집 : 이 문제가 create_line_items.rb에, 어디 것 같다 :

ActiveRecord::Schema.define(:version => 20111125123848) do 

    create_table "carts", :force => true do |t| 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "dvds", :force => true do |t| 
    t.string "title" 
    t.text  "description" 
    t.string "image_url" 
    t.decimal "price",  :precision => 8, :scale => 2 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "line_items", :force => true do |t| 
    t.integer "product_id" 
    t.integer "cart_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

end 

답변

0

스키마가 정상적으로 보입니다. 마이그레이션에 dvd_id가 누락되었습니다. 어떻게하면 스키마에서 dvd_id로 끝났습니까? 다른 마이그레이션을 추가합니까? line_items 실제로 난 그냥 더블 체크

+0

아아, rename_column을 시도하여 문제가 해결되는지 확인했습니다. (나는 위의 코드를 명확성을 위해 원래 형식으로 다시 변경했습니다.) 네가 맞다. 내 문제는 dvd_id 열 대신 product_id 열이 있다는 것입니다. 이 문제를 어떻게 해결할 수 있습니까? – Bobski

+0

rename_column이있는 다른 마이그레이션을 만들어야합니다. 그런 다음 rake db : migrate를 다시 실행하십시오. – ramblex

+0

add_column을 사용했는데 성공한 것으로 보입니다. 고맙습니다 – Bobski

1

것은 당신이 갈퀴 DB를 실행하는 것이 확인하십시오 또한

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 

, 여기에 scehma입니다 : 데이터베이스를 이주하기 위해 이주하십시오.

+0

dvd_id 열이 않습니다

sqlite3 db/development.sqlite3 sqlite3> .schema line_items 

확인, 그리고 마이그레이션을 실행 않았다

그것은 데이터베이스에 실제로 무엇을 확인하는 가치가있을 수도 있습니다. 감사합니다. – Bobski

0

Cat과 마찬가지로 마이그레이션을 실행했는지 확인하고 마이그레이션에서 열 이름을 변경하는 것을 잊어 버렸을 수도 있습니다. 내 추측으로는 과거에는 product_id였습니다. LineItem 및 현재 db/schema.rb와 관련된 이전을 게시 한 경우 도움이됩니다.

+0

당신은 돈을 받고 있습니다 : product_id. 위의 마이그레이션 및 스키마 코드로 원래 게시물을 편집했습니다. 이 문제를 어떻게 해결할 수 있습니까? 많은 분들께 감사드립니다! – Bobski

관련 문제