내가 RSpec에 일부 연결을 테스트하기 위해 노력하고있어하지만 난이 오류를 얻을 :시험 협회 : 카테고리가없는 post_id를 외래 키
class Post < ActiveRecord::Base
has_one :category
validates_presence_of :author, :category, :post
end
class Category < ActiveRecord::Base
belongs_to :post
validates_presence_of :name
end
:
여기Category
Failure/Error: it {should belong_to :post}
Expected Category to have a belongs_to association called post (Category does not have a post_id foreign key.)
# ./spec/models/category_spec.rb:7:in `block (2 levels) in <top (required)>'
Post
Failure/Error: it {should have_one (:category)}
Expected Post to have a has_one association called category (Category does not have a post_id foreign key.)
# ./spec/models/post_spec.rb:8:in `block (2 levels) in <top (required)>'
내 모델의 내 테스트 :
require 'spec_helper'
describe Post do
it {should validate_presence_of(:author)}
it {should validate_presence_of(:post)}
it {should validate_presence_of(:cathegory)}
it {should have_one (:category)}
end
describe Category do
it {should validate_presence_of :name}
it {should belong_to :post}
end
내 계획
ActiveRecord::Schema.define(:version => 20121118131441) do
create_table "categories", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "post_id"
end
create_table "posts", :force => true do |t|
t.string "author"
t.string "cathegory"
t.text "post"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "category_id"
end
end
아무도 모르게 무슨 일이 일어 났습니까?
스키마를 게시 할 수 있습니까? –
has_one/belongs_to에 대해서는 양쪽에 키가 필요하지 않습니다. 범주는 has_many posts 인 반면, 게시물은 belongs_to category (category_id 열을가집니다)라고해야합니다. –