0
我正在使用名爲Monologue的Rails博客引擎。我希望其中一個引擎的模型與我的主要應用模型擁有belongs_to和has_many關係。用戶(作者)可以有許多帖子,帖子屬於作者(用戶模型)。我嘗試在class_name中命名模型,但它仍然在引擎中搜索模型。Rails belongs_to has_many關係,主應用程序和引擎
錯誤
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
我將有調整我的應用程序代碼中的每個用戶實例都是MyApp :: User? – MicFin
@MicFin你已經在'user.rb'的'Monologue :: Post'中定義了類名:'class_name =>「MyApp :: User」,所以你需要改變'user.rb'的類名以及 – Pavan
重命名User類會打破Rails活動支持/Users/Mike/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:481:in'load_missing_constant':無法自動加載常量用戶,期望/Users/Shared/code/kindrdfood/RecRm/app/models/user.rb來定義它(LoadError) – MicFin