【发布时间】:2014-01-13 21:51:06
【问题描述】:
我有一个名为“tables”的表,里面有一个名为 table_name 的列。而且,在我的其他模式中的数据库上,我有这个表。喜欢:
db: tables
id table_name other_attributes
1 minerals ...
2 animals ...
db: minerals
id name type
1 rock nil
2 sand nil
这些表的结构不同。我想这样称呼我的模型:
> tab = Table.find(1)
> tab.elements.all
=> [ {id: 1, name: 'rock', type: nil}, {id: 2, name: 'sand', type: nil} ]
并在此 .elements 中使用任何类型的活动记录任务... 我没有找到任何方法,我找到的最接近的文章是:Rails 3/ActiveRecord: How to switch/change table name during request dynamically?
【问题讨论】:
-
只要我的 2 美分,如果你不知道如何以“railsy”的方式做到这一点。您可以随时
ActiveRecord::Base.connection.execute("SQL query")使用手动 SQL 查询创建表。
标签: ruby-on-rails ruby model ruby-on-rails-4