问题描述
所以我使用 Rails 6 构建了一个应用程序。我已经使用Rails 引擎实现了该应用的一些核心功能。
引擎的名称Baseblog,我有两个模型,分别称为作者和发布。
一个author
有多个posts
,而一个post
属于一个author
。以下是我的模型实现:
作者模型:
module Baseblog
class Author < ApplicationRecord
has_many :posts,dependent: :destroy
validates :name,presence: true
validates :address,presence: true
end
end
发布模型:
module Baseblog
class Post < ApplicationRecord
belongs_to :author
validates :name,presence: true
end
end
这是我对 作者 模型的 rspec 测试:
作者规范
require 'rails_helper'
module Baseblog
RSpec.describe Author,type: :model do
describe 'associations' do
it { is_expected.to have_many(:posts) }
end
describe "validations" do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:address) }
end
end
end
作者工厂机器人
FactoryBot.define do
factory :school do
name { "MyString" }
address { "MyText" }
end
end
帖子架构
create_table "baseblog_posts",force: :cascade do |t|
t.string "name"
t.text "description"
t.bigint "baseblog_author_id",null: false
t.datetime "created_at",precision: 6,null: false
t.datetime "updated_at",null: false
t.index ["baseblog_author_id"],name: "index_baseblog_posts_on_baseblog_author_id"
end
但是,当我对作者模型运行测试时,出现错误:
Failures:
1) Baseblog::Author associations is expected to have many posts
Failure/Error: it { is_expected.to have_many(:posts) }
Expected Baseblog::Author to have a has_many association called posts (Baseblog::Post does not have a author_id foreign key.)
# ./spec/models/baseblog/author_spec.rb:6:in `block (3 levels) in <module:Baseblog>'
Finished in 0.29222 seconds (files took 2.9 seconds to load)
6 examples,1 failure
如何定义作者模型和发布模型之间的has_many和belongs_to关联?任何形式的帮助将不胜感激。
更新:
作者 模型的测试规范在我使用 dedypuji's answer 按照以下方式修改后通过了:
作者模型
module Baseblog
class Author < ApplicationRecord
has_many :posts,class_name: 'Baseblog::Post',foreign_key: :baseblog_author_id,presence: true
end
end
发布模型
module Baseblog
class Post < ApplicationRecord
belongs_to :author,class_name: 'Baseblog::Author',foreign_key: :baseblog_author_id
validates :name,presence: true
end
end
然而,Post 模型的规范测试仍然失败:
发布规范
需要'rails_helper'
模块基础博客 RSpec.describe 帖子,输入: :model do 描述“协会”做 它 { is_expected.to own_to(:author) } 结束
describe 'validations' do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:author_id) }
end
结束 结束
用于后期规范的工厂机器人
FactoryBot.define do
factory :post do
name { "MyString" }
association :author
end
end
但是,当我为 Post 运行规范测试时,我收到以下错误:
Failures:
1) Baseblog::Post associations is expected to belong to author required: true
Failure/Error: it { is_expected.to belong_to(:author) }
NoMethodError:
undefined method `author_id' for #<Baseblog::Post:0x000056397e47dc50>
Did you mean? author
author=
# ./spec/models/baseblog/post_spec.rb:6:in `block (3 levels) in <module:Baseblog>'
Baseblog::Post validations is expected to validate that :author_id cannot be empty/falsy
Failure/Error: it { is_expected.to validate_presence_of(:author_id) }
Shoulda::Matchers::ActiveModel::AllowValueMatcher::AttributeDoesNotExistError:
The matcher attempted to set :author_id on the Baseblog::Post to
nil,but that attribute does not exist.
# ./spec/models/baseblog/post_spec.rb:19:in `block (3 levels) in <module:Baseblog>'
解决方法
你可以试试吗?
@constraint
module Baseblog
class Author < ApplicationRecord
has_many :posts,class_name: 'Baseblog::Post',foreign_key: :baseblog_author_id,dependent: :destroy
validates :name,presence: true
validates :address,presence: true
end
end