使用另一个类扩展rails模型

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?

#0

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

推荐文章

在PHP中从HTTP切换到HTTPS时会话丢失

在PHP中从HTTP切换到HTTPS时会话丢失

推荐文章

阻止jQuery自动完成插件在用户单击back时忘记文本

阻止jQuery自动完成插件在用户单击back时忘记文本

推荐文章

Linux内核(或其他低级的东西)是如何编写C语言的“好”例子吗

Linux内核(或其他低级的东西)是如何编写C语言的“好”例子吗

推荐文章

如何在对象上实现GetHashCode()?

如何在对象上实现GetHashCode()?

推荐文章

使用一个数据库调用加载业务对象层次结构

使用一个数据库调用加载业务对象层次结构

推荐文章

treeview线程安全操作

treeview线程安全操作

推荐文章

在VB/C中“生成调试信息”是什么意思?

在VB/C中“生成调试信息”是什么意思?

推荐文章

返回字符串第一次出现前后的单词

返回字符串第一次出现前后的单词

推荐文章

C++中命令行处理参数的最有效方法

C++中命令行处理参数的最有效方法

推荐文章

如何在LINQ to Sql结果中按数据对组进行lambda?

如何在LINQ to Sql结果中按数据对组进行lambda?

推荐文章

枚举特定于编译器的扩展的作用域解析运算符?

枚举特定于编译器的扩展的作用域解析运算符?

推荐文章

如何使用.NET/C进行健壮的串行端口编程?

如何使用.NET/C进行健壮的串行端口编程?

推荐文章

如何在c语言中将不可读字符串转换回UTF-8字节#

如何在c语言中将不可读字符串转换回UTF-8字节#

推荐文章

如何根据URL值在php中动态更改标记</a> </p> <p class=" rounded py-1" style="color: #888;"> 如何根据URL值在php中动态更改标记 </p> </div> </div> </div> <div class="feed-item break-all bg-white mb-2 card-item py-2 rounded-lg card-item py-2 pb-3 " style="box-shadow: 0 2px 4px 0 rgba(0,0,0,.05);"> <p class="ml-5 text-sm text-gray-600 flex items-center py-1"> 推荐文章 </p> <div class=" border-gray-400 px-4 mb-1 flex items-center"> <div class="text-base pl-2"> <p class="text-gray-900 "> <a href="/441568" class=" text-lg font-bold">什么时候可以省略include指令中的文件扩展名?</a> </p> <p class=" rounded py-1" style="color: #888;"> 什么时候可以省略include指令中的文件扩展名? </p> </div> </div> </div> <div class="feed-item break-all bg-white mb-2 card-item py-2 rounded-lg card-item py-2 pb-3 " style="box-shadow: 0 2px 4px 0 rgba(0,0,0,.05);"> <p class="ml-5 text-sm text-gray-600 flex items-center py-1"> 推荐文章 </p> <div class=" border-gray-400 px-4 mb-1 flex items-center"> <div class="text-base pl-2"> <p class="text-gray-900 "> <a href="/441576" class=" text-lg font-bold">如何使用lambda/linq从集合中提取一些数据到对象?</a> </p> <p class=" rounded py-1" style="color: #888;"> 如何使用lambda/linq从集合中提取一些数据到对象? </p> </div> </div> </div> <nav role="navigation" aria-label="Pagination Navigation" class="flex justify-between"> <span class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5 rounded-md"> « 上一页 </span> <a href="http://huati365.com/6125938?page=2" rel="next" class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 rounded-md hover:text-gray-500 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150"> 下一页 » </a> </nav> </div> </div> <div class="w-full sm:mt-2 sm:w-12/12 md:w-4/12 lg:w-4/12 "> <div class="mx-2 overflow-hidden"> <!--start--> <div style="height:700px;width:100%;"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- 侧边栏众向广告 --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-7679730964216360" data-ad-slot="7642024367" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <!--end--> </div> </div> </div> </div> <script src="http://huati365.com/highlight.js" defer></script> <script src="http://huati365.com/app.js" defer></script> <footer class="footer bg-secondary mt-10 relative"> <div class="container mx-auto px-6"> <!-- <nav class="my-3 lg:mt-0 menu-inside flex flex-col lg:flex-row items-center"> <a href="//portal.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">门户</a> <a href="//books.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">图书</a> <a href="//ebooks.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">电子书</a> <a href="//blog.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">博客</a> <a href="//yuanjie.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">猿界</a> <a href="//website.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">网址</a> <a href="//tag.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">标签云</a> <a href="//shandu.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">文学</a> <a href="//baike.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">百科</a> <a href="//data.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">数据界</a> <a href="//youda.huati365.com" class="py-4 lg:py-2 lg:px-4 sm:text-secondary opacity-75 hover:opacity-100">有答</a> </nav> --> <div class=" border-t border-teal-300 flex flex-col items-center"> <div class="sm:w-2/3 text-center py-6"> <p class="text-base mb-2">©2020-2025 天天话题 <a href="http://www.beian.miit.gov.cn/" target="_blank"><b>京ICP备14028175号</b></a></p> </div> </div> </div> </footer> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-TKLB94J2XW"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-TKLB94J2XW'); </script> <script> (function() { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <!--360 自动收录--> <script> (function(){ var src = "https://s.ssl.qhres2.com/ssl/ab77b6ea7f3fbf79.js"; document.write('<script src="' + src + '" id="sozz"><\/script>'); })(); </script> <script> document.addEventListener("DOMContentLoaded", event => { document.querySelectorAll("pre").forEach(block => { hljs.highlightBlock(block); }); }); //菜单 hover document.querySelectorAll(".nav-top > li").forEach(element => { element.addEventListener("mouseover", event => { element.childNodes[3].classList.remove("hidden"); }); element.addEventListener("mouseout", event => { element.childNodes[3].classList.add("hidden"); }); }); </script> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-138053197-1"></script> <script> window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', 'UA-138053197-1'); </script> </body> </html>