获得表名的ActiveRecord [英] Get table name from ActiveRecord
问题描述
我用的ActiveRecord :: Base.set_table_name
来对动态创建的ActiveRecord类设置我的表名。现在我需要知道如何在以后获取的价值。 API文档没有提及如何做到的事。此外,我不能导出表的名字从ActiveRecord类的名字,因为他们是有方向性不同于表名。
I used ActiveRecord::Base.set_table_name
to set my table name on a dynamically created ActiveRecord class. Now I need to know how to get that value later. The api docs don't mention anything on how to do this. Also, I can't derive the table name off the ActiveRecord class name because they are keyed differently than the table name.
下面是我在做什么一个更好的例子
Here is a better example of what I am doing
table_klass = Class.new(ActiveRecord::Base)
ActiveRecord::Base.const_set(const_name,table_klass)
app = @app
table_klass.class_eval do
after_save do
@@channel.push self
end
set_table_name t.server_table
establish_connection(
:adapter => "mysql2",
:host => app.db_host,
:username => app.db_user,
:password => app.db_pass,
:database => app.db_name
)
end
在这种情况下,如果 const_name
=测试和数据库名称数据库,应该创建一个类的ActiveRecord :: Base的:: DatabaseTest,它确实的。但是,当我打电话table_name的就可以了,我得到未定义的局部变量或方法。我是否需要调用table_name的上下课?
In this case, if the const_name
= Test and the database name is Database it should create a class of ActiveRecord::Base::DatabaseTest, which it does. But when I call table_name on it I get undefined local variable or method. Do I need to call table_name on the class?
更新:
我得到它通过调用工作 instance.class.table_name
Update:
I got it working by calling instance.class.table_name
推荐答案
您是否尝试过 table_name的
? 文档。
这篇关于获得表名的ActiveRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!