【发布时间】:2013-02-16 17:47:33
【问题描述】:
要在 Rake 任务 curretnyl 中填充一些示例数据,我有这样的方法:
def create_population_summaries
PopulationSummary.where(year: 2009).first_or_create(member_count: 12, organization_id: 1)
PopulationSummary.where(year: 2010).first_or_create(member_count: 5, organization_id: 1)
PopulationSummary.where(year: 2011).first_or_create(member_count: 99, organization_id: 1)
PopulationSummary.where(year: 2008).first_or_create(member_count: 52, organization_id: 1)
PopulationSummary.where(year: 2012).first_or_create(member_count: 30, organization_id: 1)
PopulationSummary.where(year: 2013).first_or_create(member_count: 25, organization_id: 1)
PopulationSummary.where(year: 2008).first_or_create(member_count: 46, organization_id: 1)
end
所以这意味着有一个 PopulationSummary 表,其中包含类似
的列member_count
year
organization_id
我们已经用老板的数据填充了它们!给了我。
现在我想重构它,使其使用一些 Ruby 结构,如哈希、数组,甚至哈希数组等...这样重构我的方法后 不会 在方法中包含那些手动输入的数据,并且这些手动输入的数据应该来自那个 Ruby 结构、哈希等......仍然在方法中我可以手动输入数据库字段的名称,比如年份, member_count 等...但是我想从 ruby 数据结构中读取的值..
您建议如何编写该数据结构?
【问题讨论】:
-
重构问题应该在codereview.stackexchange.com 上提问。阅读常见问题解答中的“I'm confused! What questions are on-topic for this site?”,看看它是否符合您的需求。
标签: ruby-on-rails ruby