【发布时间】:2015-10-19 16:37:19
【问题描述】:
我正在使用一个名为 Monologue 的 Rails 博客引擎。我希望其中一个引擎模型与我的主要应用程序模型具有belongs_to 和has_many 关系。一个用户(作者)可以有很多帖子,一个帖子属于一个作者(用户模型)。我尝试在 class_name 中为模型命名空间,但它仍在引擎中搜索模型。
错误
NameError: uninitialized constant Monologue::Post::MyApp::User
post.rb
class Monologue::Post < ActiveRecord::Base
belongs_to :author, :class_name => "MyApp::User", :foreign_key => "author_id"
end
user.rb
class User < ActiveRecord::Base
has_many :posts, :class_name => "Monologue::Post", :foreign_key => "author_id"
end
架构
create_table "monologue_posts", force: true do |t|
t.integer "author_id"
end
我已经使用了:Creating a belongs_to relationship with a model from the main app from an engine model
【问题讨论】:
标签: ruby-on-rails-4 activerecord foreign-key-relationship rails-models monologue