在运行时创建ActiveRecord模型rails3
我想在运行时创建一个 ActiveRecord 模型。
在我的系统中,我生成一些报告,所有报告都与数据库表/视图绑定。所以唯一的区别是表名。
所以我想做的是有一个活动记录公共类(最好在运行时生成)并在运行时分配表名。我应该能够指定一个范围来选择日期范围。我正在考虑在运行时获取列名称来显示数据。
- 谁能帮助我如何 在运行时创建 ActiveRecord 模型
- 分配范围方法
,我在 Rails3 上,
提前致谢,
欢呼
Sameera
I want to create an ActiveRecord model at run time.
In my system I'm generating some reports and all of them bind with a database table/ views. So the only difference will be the table name.
So what I want to do is have an active record common class (preferably generated at runtime) and assign the table name at runtime. and i should be able to assign a a scope to select a date range. And I'm thinking of getting the column names at runtime to display data.
- can anyone help me out on how to
create ActiveRecord model at runtime - assign scope method
and I'm on rails3
thanks in advance
cheers
sameera
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您链接的文章中借用
create_class
,这样的事情怎么样:如果您想使用数据库中的所有表,您可以这样做:
但是除非您以相同的方式对待所有表,否则我我猜你不想这样做。
创建模型类后,您现在应该能够打印每个表中的对象及其各自的列:
Borrowing
create_class
from the article you linked, how about something like this:If you wanted to use all tables in the database, you could do this:
But unless you're treating all tables identically, I'm guessing you wouldn't want to do that.
Once you've created your model classes, you should now be able to print objects from each table with their respective columns:
假设视图名称是静态的,执行此操作的最简单方法是创建一个具有通用功能的模块,然后将其包含到您的基础模型中
。
...
结束
类 A 扩展了 ActiveRecord::Base
包括模块A
B 类扩展
了 ActiveRecord::Base
包括模块A
结尾
The easiest way to do this assuming the view names are static is to create one module with the common functionality and then include it into your base models..
module ModuleA
...
end
class A extends ActiveRecord::Base
include ModuleA
end
Class B extends ActiveRecord::Base
include ModuleA
end