Rails 引擎中的关联不工作
Associations within Rails Engine not working
我正在构建我的第一个 Rails 引擎并且已经很困惑已经定义了两个模型之间的关联。为了方便起见,假设引擎的名称是 Blorg,有两个模型 Article 和 Author.
blorgh/article.rb
module Blorgh
class Article < ApplicationRecord
belongs_to :author, class_name: 'Blorg::Author',
foreign_key: 'blorg_author_id', optional: true
blorgh/author.rb
module Blorgh
class Author < ApplicationRecord
has_many :articles, class_name: 'Blorg::Article'
架构
create_table "blorgh_authors", force: :cascade do |t|
t.string "name"
t.boolean "inactive", default: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
create_table "blorgh_articles", force: :cascade do |t|
t.string "title"
t.bigint "blorgh_author_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["blorgh_author_id"], name: "index_blorgh_article_on_author_id"
如果我尝试通过 rails c 创建文章或计算作者的文章,我会收到这些错误:
article = Blorgh::Article.new(title: 'New Article')
article.save # expect true
# ==> NoMethodError: private method `attribute' called for #<Blorgh::Article:0x00007fdc3fad4d50>
author = Blorgh::Author.create # worked
author.articles.count # expect 0
# ==> ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column blorgh_articles.author_id does not exist
有谁知道我如何才能正确实现这些引擎内关联?
第二个错误(“列 blorgh_articles.author_id 不存在”)是因为 Rails 假设 has_many
关系上的外键是 classname_id,在您的示例中为 author_id
。您在 belongs_to
侧正确设置了外键,但您需要在关系的两侧指定。所以:
module Blorgh
class Author < ApplicationRecord
has_many :articles, class_name: 'Blorg::Article', foreign_key: 'blorg_author_id'
...
我正在构建我的第一个 Rails 引擎并且已经很困惑已经定义了两个模型之间的关联。为了方便起见,假设引擎的名称是 Blorg,有两个模型 Article 和 Author.
blorgh/article.rb
module Blorgh
class Article < ApplicationRecord
belongs_to :author, class_name: 'Blorg::Author',
foreign_key: 'blorg_author_id', optional: true
blorgh/author.rb
module Blorgh
class Author < ApplicationRecord
has_many :articles, class_name: 'Blorg::Article'
架构
create_table "blorgh_authors", force: :cascade do |t|
t.string "name"
t.boolean "inactive", default: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
create_table "blorgh_articles", force: :cascade do |t|
t.string "title"
t.bigint "blorgh_author_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["blorgh_author_id"], name: "index_blorgh_article_on_author_id"
如果我尝试通过 rails c 创建文章或计算作者的文章,我会收到这些错误:
article = Blorgh::Article.new(title: 'New Article')
article.save # expect true
# ==> NoMethodError: private method `attribute' called for #<Blorgh::Article:0x00007fdc3fad4d50>
author = Blorgh::Author.create # worked
author.articles.count # expect 0
# ==> ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column blorgh_articles.author_id does not exist
有谁知道我如何才能正确实现这些引擎内关联?
第二个错误(“列 blorgh_articles.author_id 不存在”)是因为 Rails 假设 has_many
关系上的外键是 classname_id,在您的示例中为 author_id
。您在 belongs_to
侧正确设置了外键,但您需要在关系的两侧指定。所以:
module Blorgh
class Author < ApplicationRecord
has_many :articles, class_name: 'Blorg::Article', foreign_key: 'blorg_author_id'
...