如何在Rails 3的模型类内确定表名 [英] How to determine table name within a Rails 3 model class

查看:126
本文介绍了如何在Rails 3的模型类内确定表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个模型法表名。我发现应该有方法table_name的,但是当我尝试调用它,我得到NameError异常:未定义的局部变量或方法'TABLE_NAME。这显然​​是不存在:

 第methods.grep(/ ^ TA。* /)
[table_name_ preFIX?,
 table_name_suffix?,
 污点,
 taguri
 taguri =,
 污点?,
 table_name_ preFIX
 table_name_suffix
 挖掘]
 

如何获得真正的表名?(没有lowecase - 复数的技巧)

感谢

解决方案
  

但我需要的信息   模型的实例方法。如何获得   它?

您可以简单地做到这一点在你的实例方法:

 类模型
  高清instance_method
    把Model.table_name
  结束
结束
 

I want to get table name in a model method. I found there should be method table_name but when I try to call it I get NameError Exception: undefined local variable or method `table_name'. It is obviously not there:

 pp methods.grep(/^ta.*/)
["table_name_prefix?",
 "table_name_suffix?",
 "taint",
 "taguri",
 "taguri=",
 "tainted?",
 "table_name_prefix",
 "table_name_suffix",
 "tap"]

How to get a "real" table name (no lowecase - pluralize tricks)?

Thanks

解决方案

But I need that information in the model's instance method. How to get it?

You can simply do this in your instance method:

class Model
  def instance_method
    puts Model.table_name
  end
end

这篇关于如何在Rails 3的模型类内确定表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆