我想通過where查詢定義一個新的數組,但我只能讓它工作一種方式。收集返回由於.where沒有如預期的響應
這就是:
<%
@children = Array.new
Topic.where(breadcrumb_id: @topic.id).each do |y|
@children.push(y.name)
end
return @children
%>
Returns the array ["Performance Arts", "Visual Arts", "Physical Arts", "Music", "Culinary Arts"] (All topics)
但我寧願只是做
@children = Topic.where(breadcrumb_id: @topic.id)
return @children.each.name
Returns "undefined method `name' for #<Enumerator:0x007fe92205a1f0>"
無論出於何種原因。每次將無法正確響應......雖然它適用於初次通話地方在第一個例子中。有什麼不同?
有沒有辦法做到這一點,以便我可以通過數組直接拉名稱?
謝謝一堆拉爾斯。我被困在此約2小時。 –
您無法調用'Topic.where(breadcrumb_id:@ topic.id).to_a.name',這裏沒有ActiveRecord :: Relation的問題。 –