Extend a rails model using another class
I'm writing a gem that I want to extend a model. I tried defining the class in my gem as:
class UserModel < ActiveRecord::Base
and then the user model as:
class User < Adauth::UserModel
But this caused Active Record to throw up a table not found 开发者_开发知识库error as it was using UserModel not User for the model name.
I can't specify the the model name as I intend to have a named generator create the model with User as the default.
I'm assuming that it isnt possible to inherit from 2 classes/modules in the definition line so how would I import all the methods from Adauth::UserModel into a model?
I don't know if this is the best solution for your problem, but you could try using mixins.
Define a module UserModel with all the methods you want.
module Adauth
module UserModel
# methods go here
end
end
and then when you define your model:
class User < ActiveRecord::Base
include Adauth::UserModel
end
精彩评论