我想在Rails應用程序的方法中添加一個循環。它看起來像這樣如何在一個方法內循環?
Parent.do_something(
attribute: "a string",
parameter: "a string",
child[0]: "child_url"
)
有時父母沒有孩子。有時父母會有x個孩子。我如何在一個循環遍歷所有這些孩子的函數中創建一個循環。
我想是這樣
i=0
children=Child.all
Parent.do_something(
attribute: "a string",
parameter: "a string",
for child in children
child[i]: "child_url"
i= i + 1
end
)
,將產生
Parent.do_something(
attribute: "a string",
parameter: "a string",
child[0]: "child_0_url",
child[1]: "child_1_url",
child[2]: "child_2_url"
)
如果我沒有解釋的很清楚這個問題,我會更新基於評論我的問題。
你真的需要每個孩子的鑰匙嗎?或者你只是想獲得一個child_urls數組? – 2013-04-23 04:01:50
你究竟想要完成什麼?現在它看起來像你試圖添加一個可變數量的條目到散列然後傳遞給類方法 – AJcodez 2013-04-23 04:05:38
是的,我需要每個條目的一個鍵。我已經更新了這個問題,對不清楚的道歉,我正在努力解決這個問題(甚至是否有可能) – 2013-04-23 04:20:05