在运行时创建ActiveRecord模型rails3

发布于 2024-11-07 01:28:34 字数 301 浏览 0 评论 0原文

我想在运行时创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我三岁 2024-11-14 01:28:34

从您链接的文章中借用 create_class ,这样的事情怎么样:

TABLE_NAMES = %w[customers products purchases]

def create_class(class_name, superclass, &block)
  klass = Class.new superclass, &block
  Object.const_set class_name, klass
end

def create_model_class(table_name)
  create_class(table_name.classify, ActiveRecord::Base) do
    scope :in_date_range, lambda {|start_date, end_date| where("start_date >= ?", start_date).where("end_date <= ?", end_date) }
  end
end

TABLE_NAMES.each {|table_name| create_model_class(table_name) }

如果您想使用数据库中的所有表,您可以这样做:

TABLE_NAMES = ActiveRecord::Base.connection.tables

但是除非您以相同的方式对待所有表,否则我我猜你不想这样做。

创建模型类后,您现在应该能够打印每个表中的对象及其各自的列:

def print_attributes(model_obj)
  puts model_obj.attributes.map {|k,v| "#{k}=#{v.inspect}" }.join(', ')
end

Customer.in_date_range(Date.today, Date.today + 1.day).each {|c| print_attributes(c) }
Product.in_date_range(Date.yesterday, Date.today + 2.days).each {|c| print_attributes(c) 
Purchase.in_date_range(Date.yesterday, Date.today + 3.days).each {|c| print_attributes(c) }

Borrowing create_class from the article you linked, how about something like this:

TABLE_NAMES = %w[customers products purchases]

def create_class(class_name, superclass, &block)
  klass = Class.new superclass, &block
  Object.const_set class_name, klass
end

def create_model_class(table_name)
  create_class(table_name.classify, ActiveRecord::Base) do
    scope :in_date_range, lambda {|start_date, end_date| where("start_date >= ?", start_date).where("end_date <= ?", end_date) }
  end
end

TABLE_NAMES.each {|table_name| create_model_class(table_name) }

If you wanted to use all tables in the database, you could do this:

TABLE_NAMES = ActiveRecord::Base.connection.tables

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:

def print_attributes(model_obj)
  puts model_obj.attributes.map {|k,v| "#{k}=#{v.inspect}" }.join(', ')
end

Customer.in_date_range(Date.today, Date.today + 1.day).each {|c| print_attributes(c) }
Product.in_date_range(Date.yesterday, Date.today + 2.days).each {|c| print_attributes(c) 
Purchase.in_date_range(Date.yesterday, Date.today + 3.days).each {|c| print_attributes(c) }
逆夏时光 2024-11-14 01:28:34

假设视图名称是静态的,执行此操作的最简单方法是创建一个具有通用功能的模块,然后将其包含到您的基础模型中


...
结束

类 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文