我想在模型中覆盖Lib模型并添加关系。 最好的方法是什么?
rpush lib中的模型示例:
module Rpush
module Client
module ActiveRecord
class App < ::ActiveRecord::Base
self.table_name = 'rpush_apps'
if Rpush.attr_accessible_available?
attr_accessible :name, :environment, :certificate, :password, :connections, :auth_key, :client_id, :client_secret
end
has_many :notifications, class_name: 'Rpush::Client::ActiveRecord::Notification', dependent: :destroy
validates :name, presence: true, uniqueness: { scope: [:type, :environment] }
end
end
end
end
我想在不编辑gem的情况下添加has_many关系 所以我想用这个创建一个models / app.rb就是一个开始:
class Rpush::Client::ActiveRecord::App
has_many :rel_group_apps
has_many :groups, :through => :rel_group_apps
end
我试过这个,但没有改变。也许我的模型/ app.rb没有被调用?:
module Rpush
module Client
module ActiveRecord
module App
def self.included(includer)
includer.class_eval do
has_many :rel_group_apps
has_many :groups, :through => :rel_group_apps
end
end
end
end
end
end
我该怎么办?有没有办法在不删除原始行为的情况下扩展lib模型?
谢谢!
修改
我使它工作但只是将此代码直接放在config / initializers / rpush.rb中 它不适用于models / app.rb
class Rpush::Client::ActiveRecord::App
has_many :rel_group_apps
has_many :groups, :through => :rel_group_apps
end
如果有人有更好的想法,我会接受它!
答案 0 :(得分:0)
使用类&lt;&lt;扩展类自
class Rpush::Client::ActiveRecord::App
class << self
[your methods here]
end
end