Rails:Belongs_to 和 Has_many 关联在 Rails 引擎中

Rails: Belongs_to and Has_many associations in Rails Engines

所以我使用 Rails 6 构建了一个应用程序。我已经使用 Rails Engines.

实现了该应用程序的一些核心功能

引擎的名称 Baseblog,我有 2 个模型,分别是 AuthorPost.

一个author有多个posts,而一个post属于一个author。以下是我的模型实现:

作者模型:

module Baseblog
  class Author < ApplicationRecord
    has_many :posts, dependent: :destroy

    validates :name, presence: true
    validates :address, presence: true
  end
end

Post 型号:

module Baseblog
  class Post < ApplicationRecord
    belongs_to :author

    validates :name, presence: true
  end
end

这是我对 Author 模型的 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

Posts

的架构
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", precision: 6, null: false
  t.index ["baseblog_author_id"], name: "index_baseblog_posts_on_baseblog_author_id"
  end

但是,当我 运行 我对 Author 模型进行测试时,我得到了错误:

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

如何定义 Author 模型和 Post[= 之间的 has_many 和 belongs_to 关联94=]型号?任何形式的协助都将不胜感激。

更新:

Author 模型的测试规范在我使用 :

修改后现在通过了

作者模型

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

Post型号

module Baseblog
  class Post < ApplicationRecord
    belongs_to :author, class_name: 'Baseblog::Author', foreign_key: :baseblog_author_id

    validates :name, presence: true
  end
end

但是,Post 型号的规范测试仍然失败:

Post规格

需要'rails_helper'

模块Baseblog RSpec.describe Post,输入::model do 描述 'associations' 做 它 { is_expected.to belong_to(:author) } 结束

describe 'validations' do
  it { is_expected.to validate_presence_of(:name) }
  it { is_expected.to validate_presence_of(:author_id) }
end

结束 结束

Post 规格的工厂机器人

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>'

你能试试这个吗?

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
module Baseblog
  class Post < ApplicationRecord
    belongs_to :author, class_name: 'Baseblog::Author', foreign_key: :baseblog_author_id

    validates :name, presence: true
  end
end