2014-01-09 22 views
0

脚本我有运行:我必须多次运行此脚本。有没有我能写的脚本可以一次运行所有内容?

Li = LineItem.find ##### 
li.rental_period = ### 
li.save! 

我有800个项目编号为运行此。你的帮助将不胜感激。

+0

是它为所有这些相同rental_period? rental_period是您需要初始化还是仅仅是一个字符串? – jstim

+0

相同的租赁期限不同的行项目编号。 – user3178864

+1

这应该用Rails还是ActiveRecord来标记?这看起来不仅仅是香草红宝石。 – Max

回答

0
line_item_number_string = line_item_number.join(',')  
ActiveRecord::Base.connection.execute("UPDATE line_items SET rental_period=<value> WHERE id IN (#{line_item_number_string})") 
+0

谢谢你们的帮助!!! ^^^ – user3178864

1

如果它们都具有不同的租期:

line_item_numbers.each do |num| 
    li = LineItem.find(num) 
    li.rental_period = custom_rental_period 
    li.save! 
end 

如果他们都是一样的,你可以做一个update_all

LineItem.where(id: line_item_numbers).update_all(rental_period: custom_rental_period) 
0

while循环

a = 0 
b = 4 #this loop will run 4 times 
while a < b 

Li = LineItem.find ##### 
li.rental_period = ### 
li.save! 

a += 1 #adds one to a 
end 

这很漂亮与第一个答案非常相似,只是以不同的方式写成。我发现红宝石的noobs可以更容易理解这一点。

OR

def go 
Li = LineItem.find ##### 
li.rental_period = ### 
li.save! 
end 

然后调用 “走出去”,在你的代码来执行你的语法

go 

这就是你打电话怎么走