我有一个遗留的表,如下所示
item-detail-table
some-key,
item-1,
description-1,
item-2,
description-2,
item-3,
description-3,
item-4,
description-4,
item-5,
description-5,
other-fielda,
other-fieldb,
etc.我想让我的Rails模型返回五个独立的对象,以响应find_by_some_key检索到的每个匹配记录,每个记录都具有以下逻辑结构:
normalized-item-detail
some-key,
item,
description,
other-fielda,
other-fieldb,
etc.我只是以前没有在Rails中做过这样的事情,我想知道做这样的事情的最好/最惯用的方式。
它将是一个只读模型,因此我可以避免需要能够更新结果对象的所有复杂性。
我是否应该有一个表示单个子条目的中间模型,然后让这个模型返回一个数组,这些数组按照我想要的方式发出嘎嘎声?
谢谢!
G.
发布于 2012-12-09 08:43:52
您可以创建一个TableItem-Model,它有一个返回规范化类的方法……
像这样:
def TableItem < ActiveRecord::Base
class NormalizedItem
#attributes
end
def getNormalizedItems
ret = []
3.times do |u|
....
ret << NormalizedItem.new(params)
end
return ret
end
endhttps://stackoverflow.com/questions/13783265
复制相似问题