Anish
Anish

Reputation: 558

value not stored in array ruby on rails

I initialize array

k = Array.new

then something like

@m = Modul.where(:rfp_id => @rfp.id)
i = 0
for ii in @m
  k = ii.name.to_s
  i = i + 1
end 

and then

 k.each do |a|
    ...
 end

but value is not stored in "k" how can i do this?

Upvotes: 1

Views: 92

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118299

Change this k = ii.name.to_s to k.push(ii.name.to_s). It will work. You did not anywhere add the item ii.name.to_s to your array k. k = ii.name.to_s means in each loop iteration, you are going to reference a new string object to the local variable k. Read Array#push to get introduce yourself to the method.

Don't need to write k = Array.new, ratherk = [] is enough for your purpose.

Upvotes: 1

xdazz
xdazz

Reputation: 160963

Try write clean code, for example:

Modul.where(:rfp_id => @rfp.id).pluck(:name).each do |name|
  puts name
end

Upvotes: 3

Related Questions