Rails Engines – 简单可能的引擎(1)添加模型和(2)在包含类中添加关联
我正在尝试编写我的第一个引擎,但遇到以下情况的问题。 在主机应用程序中,我将拥有User模型(这在引擎中可以保证,所以我可以引用User类而不是某种程度的间接),它有一个名称。 在引擎中,我将有一个post模型,需要在包含应用程序中的post模型和用户模型之间创建关联。
>rails plugin new abc --mountable ... >rails g model Post header:string body:text user_id:integer
…编辑post文件:
module Abc class Post < ActiveRecord::Base attr_accessible :body, :header, :user_id belongs_to :user def self.say_hello puts "hello: " + Object.const_get(User).to_s end end end
如何访问包含类中的用户模型以添加以下内容?
has_many :abc_posts, class: "Abc::Post"
我已经尝试了引擎中的每个变体(在app / models / app / models / abc等……):
class User < ActiveRecord::Base has_many :abc_posts, class: "Abc::Post" end
但无济于事。 我如何让它工作?
提前thx