I read that initialitation of array in ruby is like myarray = [apple.txt, house.txt]
How can I store the value of a table in an array
if !haus.blank?
#from below, I will get a list of haus.name that I need to store in an array
haus.each do |f|
hausname = haus.name
end
end
i need to store each of the haus.name that I get from iterating in haus table to
myarray=[listofhaus.name开发者_Python百科]How can I do this in ruby?
Thank you for your help
You can get all the array of names using the map
myarray = haus.map {|f| f.name} or
myarray = haus.map(&:name)
myarray = haus.collect(&:name)
I take i you're on Rails. Since your each iteration doesn't really make sense, here's a generic example:
Haus.all.map {|h| h.name }
This get's all objects of model Hause and maps the name attribute of each into an array.
精彩评论