0

I want to add an association to my Comment class from a different location than comment.rb, e.g. in an initializer. Something like:

# in config/initializers/comment_setup.rb
class Comment < ActiveRecord::Base
  has_many :replies
end

But when I try this (rails 2.3.4) I get an error that the has_many association does not exist.

What's the best way to re-open an ActiveRecord class from a location other than app/models/blah.rb?

4

1 回答 1

0

看到您的评论后,我认为您的做法是错误的。看看多态关联,http ://guides.rubyonrails.org/association_basics.html#polymorphic-associations

这是一种通过存储关联类型(即模型的类名)以及 ID 来将一个模型关联到多个模型的方法。这就是您在多个模型中重用关联的方式。

于 2012-05-21T18:31:45.030 回答