2014-01-09 21 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 

這就是你打電話怎麼走