2016-11-16 1 views
1

에서 일어나는 내가 TopicList (부모)와 주제 (아이) 모델알 수없는 속성 오류가 만 테스트

class TopicList < ApplicationRecord 
    has_many :topics 
    validates :name, presence: true , uniqueness: { case_sensitive: false } 
end 

class Topic < ApplicationRecord 
    belongs_to :topic_list 
    validates :topic_list_id, presence: true 
    validates :name, presence: true, uniqueness: { case_sensitive: false } 
end 

schema.rb는 다음과 같습니다

ActiveRecord::Schema.define(version: 20161116031922) do 

    create_table "topic_lists", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.index ["name"], name: "index_topic_lists_on_name", unique: true 
    end 

    create_table "topics", force: :cascade do |t| 
    t.string "name" 
    t.string "source" 
    t.integer "position" 
    t.integer "topic_list_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.index ["name"], name: "index_topics_on_name", unique: true 
    t.index ["position"], name: "index_topics_on_position" 
    t.index ["topic_list_id"], name: "index_topics_on_topic_list_id" 
    end 

내 topic_test.rb을 새로운 TopicList를 생성하고 그 아래 항목을 구축하여 시작 : 나는이 테스트를 실행할 때마다

require 'test_helper' 

class TopicTest < ActiveSupport::TestCase 
    def setup 
    @topic_list = TopicList.new(name: "Vocab") 
    @topic_list.save 
    @topic = @topic_list.topics.build(name: "Topic 1") 
    end 

    test "should be valid" do 
    assert @topic.valid? 
    end 
end 

, 나는 오류 :

ActiveModel::UnknownAttributeError: unknown attribute 'topic_list_id' for Topic. 

하지만 내 레일 콘솔에서 동일한 순서로 시도해도 정상적으로 작동합니다.

Loading development environment (Rails 5.0.0.1) 
>> list = TopicList.new(name: "List 1") 
=> #<TopicList id: nil, name: "List 1", created_at: nil, updated_at: nil> 
>> list.save 
    (0.1ms) begin transaction 
    TopicList Exists (0.3ms) SELECT 1 AS one FROM "topic_lists" WHERE LOWER("topic_lists"."name") = LOWER(?) LIMIT ? [["name", "List 1"], ["LIMIT", 1]] 
    SQL (0.3ms) INSERT INTO "topic_lists" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "List 1"], ["created_at", 2016-11-16 21:23:33 UTC], ["updated_at", 2016-11-16 21:23:33 UTC]] 
    (11.3ms) commit transaction 
=> true 
>> topic = list.topics.build(name: "TopiC#1") 
=> #<Topic id: nil, name: "TopiC#1", source: nil, position: nil, topic_list_id: 3, created_at: nil, updated_at: nil> 
>> topic.valid? 
    Topic Exists (0.2ms) SELECT 1 AS one FROM "topics" WHERE LOWER("topics"."name") = LOWER(?) LIMIT ? [["name", "TopiC#1"], ["LIMIT", 1]] 
=> true 
>> 

내가 여기 발견 한 모든 유사한 질문에는 잘못된 CamelCase, snake_case 또는 복수/단일 이름이 포함됩니다. 나는 내가 여기 모든 것을 올바르게했다고 생각한다. 내가 뭘 놓치고 있니?

+0

테스트 데이터베이스가 올바르게 구성 되었습니까? 오류의 전체 스택 추적을 붙여 넣을 수 있습니까? –

답변

0

나는 rails db:test:load을 실행했으며이를 정리했습니다. 왜 이것이 때때로 필요하지만 다른 이유가 필요한지 잘 모릅니다.