Rails Engines - simple possible engine to (1) add a model and (2) add the association in the containing class

这一生的挚爱 提交于 2019-12-12 21:23:24

问题


I am trying to write my first engine but am having problems with the following scenario. In the host app, I will have User model (this is guaranteed so in the engine I can refer to User class rather than some level of indirection) which has a name. In the engine, I will have a post model and need to create an association between the post model and the user model in the containing app.

>rails plugin new abc --mountable
...
>rails g model Post header:string body:text user_id:integer

... edit the post file:

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

How do I access the user model in the containining class to add the following?

has_many :abc_posts, class: "Abc::Post"

I have tried every variation of having in the engine (at app/models/ app/models/abc etc....):

class User < ActiveRecord::Base
  has_many :abc_posts, class: "Abc::Post"
end

but to no avail. How do I get this to work?

thx in advance

来源:https://stackoverflow.com/questions/16349322/rails-engines-simple-possible-engine-to-1-add-a-model-and-2-add-the-associ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!