ruby-on-rails – 如何在关注点(或模块中)访问模型名称?

ruby-on-rails – 如何在关注点(或模块中)访问模型名称?,第1张

概述我在rails应用程序中有以下问题: module Authenticable extend ActiveSupport::Concern included do # ... end module ClassMethods def quoted_table_name self.class.name.downcase.pluralize # retur 我在rails应用程序中有以下问题:

module Authenticable  extend ActiveSupport::Concern  included do    # ...  end  module ClassMethods    def quoted_table_name      self.class.name.downcase.pluralize # returns "classes"    end  endend

如果我有一个用户类:

class User  include Authenticableend

然后我可以像User.quoted_table_name一样返回“用户”.目前,User.quoted_table_name返回“类”.我也试过以下,但没有改变.

def quoted_table_name  Proc.new { self.class.name.downcase.pluralize }.callend
解决方法 请尝试以下方法:

module Authenticable  ...  module ClassMethods    def quoted_table_name      name.downcase.pluralize # returns "users"    end  endend

self.class.name.downcase.pluralize不起作用,因为我们已经在User-class的类端 *** 作.在向User-class询问其类时,返回Class.但是,Class.name.downcase.pluralize是“类”.

总结

以上是内存溢出为你收集整理的ruby-on-rails – 如何在关注点(或模块中)访问模型名称?全部内容,希望文章能够帮你解决ruby-on-rails – 如何在关注点(或模块中)访问模型名称?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1294413.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存