如何确定 Rails 3 模型类中的表名称
我想在模型方法中获取表名。我发现应该有方法table_name,但是当我尝试调用它时,我得到了NameError 异常:未定义的局部变量或方法“table_name”。显然不存在:
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 -pluralizericks)?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在实例方法中简单地执行此操作:
You can simply do this in your instance method:
找到了。
这是一个类方法。从 Rails 3 文档来看,这一点并不那么明显。
Found it.
It's a class method. Its not so obvious from the Rails 3 documentation.
如果您处于需要其表名称的类的类方法中,请尝试:
如果您尝试使用,
您将遇到
NoMethodError: undefined method 'table_name' for Class:Class
If you are in a class method of the class you want the table name for, try:
If you try using
you'll run into a
NoMethodError: undefined method 'table_name' for Class:Class