如何在不更改原始类的情况下替换belongs_to关联

时间:2013-07-24 20:51:10

标签: ruby-on-rails ruby metaprogramming rails-activerecord refinerycms

我有一个看起来像这样的课程:

class Refinery::Blog::Post < ActiveRecord::Base
  belongs_to :author, class_name: '::Refinery::User', foreign_key: :user_id

  # SNIP very long class body
end

如果我可以提供帮助,我不想覆盖整个类,因为那里有大量的行为应该保持不变,引擎的许多其他部分按名称引用类,所以我不能将它子类化。有没有办法使用class_eval删除或替换现有关联?我试着像这样重新定义它:

Refinery::Base::Post.class_eval do
  belongs_to :author, class_name: 'MyUser', foreign_key: :user_id
end

但它似乎不起作用。

1 个答案:

答案 0 :(得分:0)

查看初始化文件,我认为它位于config / initializers / refinery / blog.rb

Refinery::Blog.configure do |config|


  # If you're grafting onto an existing app, change this to your User class
   Refinery::Blog.user_class = 'MyUser'
end