Ruby on rails Rails:属于Rails引擎,并且在Rails引擎中有许多关联

Ruby on rails Rails:属于Rails引擎,并且在Rails引擎中有许多关联,ruby-on-rails,ruby,rails-engines,Ruby On Rails,Ruby,Rails Engines,因此,我使用Rails 6构建了一个应用程序。我已经使用Rails引擎实现了应用程序的一些核心功能 引擎的名称是Baseblog,我有两个模型,分别称为Author和Post 一个作者有许多帖子,而帖子属于一个作者。以下是我的模型实现: 作者模型: module Baseblog class Author < ApplicationRecord has_many :posts, dependent: :destroy validates :name, presence

因此,我使用Rails 6构建了一个应用程序。我已经使用Rails引擎实现了应用程序的一些核心功能

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

一个
作者
有许多
帖子
,而
帖子
属于一个
作者
。以下是我的模型实现:

作者模型

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
作者工厂Bot

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", precision: 6, 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
结束 结束

工厂Bot用于后期规范

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 :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>'
故障:
1) Baseblog::文章关联应属于作者必需:true
失败/错误:它{应该属于(:作者)}
命名错误:
未定义的方法“author\u id”#
你是说?作者
作者=
#./spec/models/baseblog/post_spec.rb:6:in'block(3层)in'
Baseblog::Post验证应验证以下内容:author_id不能为空/falsy
失败/错误:应为{验证是否存在(:author\u id)}
Shoulda::Matchers::ActiveModel::AllowValueMatcher::AttributeDesNotexistError:
匹配器试图将Baseblog::Post上的:author_id设置为
nil,但该属性不存在。
#./spec/models/baseblog/post_spec.rb:19:in'block(3层)in'
你能试试这个吗

modulebaseblog
类作者<应用程序记录
有很多:帖子,类名:'Baseblog::Post',外键::Baseblog\u author\u id,依赖::destroy
验证:名称,状态:true
验证:地址,状态:true
结束
结束
modulebaseblog
类Post
Baseblog::Post中是否有作者id?是的。我只是把它添加到问题中,这对我很有效。非常感谢你。我想这里需要注意的是,Author模型和Post模型的
外键
是相同的。是的,我也有同样的问题,我这样做是因为rails可能必须为模块中的模型指定类名和外键。也许您可以找到更好的解决方案我在测试时遇到了一些其他问题,并且我已经更新了问题@dedypuji,您可以提供帮助吗?如果您直接使用id分配,请使用baseblog\u author\u id,而不是使用author\u id;如果您使用baseblog::author实例,则可以使用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
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>'