问题
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