简体   繁体   English

需要在多个Rails模型上运行任务。 我可以使用数组做循环吗?

[英]Need to run tasks on several rails models. Can I use an array do loop?

I have a rake task in which I operate on one model. 我有一个耙任务,我在一个模型上进行操作。 It updates and deletes stuff in the table. 它更新和删除表中的内容。 I have identical tables in other databases for which i created other models. 我在为其创建其他模型的其他数据库中拥有相同的表。 How can I run the same tasks on those other models without duplicating code and keeping things DRY? 如何在其他模型上运行相同的任务,而无需复制代码并使事物保持干燥?

for example here is my code: 例如,这是我的代码:

# Category is the model
new_region = Category.find_or_initialize_by_code(:code)
... 
...
new_region.save!

I want to be able to do the same thing with another model called Hierarchy But I don't want to duplicate code like this: 我希望能够使用另一个名为Hierarchy模型执行相同的操作,但是我不想重复这样的代码:

# Hierarchy is the model
new_cat_region = Hierarchy.find_or_initialize_by_code(:code)
... 
...
new_cat_region_cat.save!

Is there a way I can create an array of models and loop through them like this? 有没有办法创建一个模型数组并像这样遍历它们?

   my_models = ['Category', 'Hierarchy']

   my_models.each do |model_name|
     a_region = model_name.find_or_initialize_by_code(:code)
       ... 
       ...
     a_region.save!

How would the strings in the array be treated? 数组中的字符串将如何处理? Should this be okay? 这样可以吗 I feel uncomfortable with the type casting that may be happening behind the scenes in rails. 我对在Rails幕后进行的类型转换感到不舒服。

You're almost there, just use the class objects directly, like so: 您快到了,只需直接使用类对象,如下所示:

my_models = [Category, Hierarchy]

my_models.each do |klass|
  a_region = klass.find_or_initialize_by_code(:code)
    ... 
    ...
  a_region.save!
end

You can do this as, 您可以这样做,

my_models = ['Category', 'Hierarchy']

my_models.each do |model_name|
 a_region = model_name.constantize.find_or_initialize_by_code(:code)
   ... 
   ...
 a_region.save!

constantize will convert string into class constantize将字符串转换为类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何运行使用Rails模型的Ruby任务? - How do I run Ruby tasks that use my Rails models? 如何使用 Rails clockwork gem 运行 rake 任务? - How do I use Rails clockwork gem to run rake tasks? 用一个属于两个模型的模型。 为什么我可以在Rails控制台中创建一个没有ID的对象,但是会通过规格测试进行回滚? - With a model that belongs_to two models. Why can I create an object with no ID in the Rails console, but will rollback with a spec test? 创建模型之间的关系。 Rails 3 - Creating relationship between models. Rails 3 我的模型有一个模型列表。 如何从数据库加载它但限制列表中的项目数? - My model has a list of models. How do I load it from the database but limit the number of items in the list? 在Rails中,如何在关机时运行某些任务? - In Rails, how do I run certain tasks on shutdown? 如何在rails应用程序中运行rake任务 - How do I run rake tasks within my rails application 使用ActiveRecord上传图片需要多少个Rails模型? - How many rails models do I need for a picture upload with ActiveRecord? 如何在OpenShift上的“无论何时/执行任务”运行rails? - How can I run my rails “whenever”/cron tasks on OpenShift? 重新打包来自多个模型的查询结果。 铁轨 - Repacking query result from multiple models. Rails-way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM