I am new to RoR. I was trying to find a way over googling to concatenate the string in a loop in Controller.
assets = Asset.where({ :current_status => ["active"] }).all
assets.each do |a|
string = string + ":"+ a.movie_title
end
I want to concatenate attribute "movie_title" as a string that would be colon separated.
but i get error
undefined method `+' for nil:NilClass
The easiest way is probably:
string = assets.collect(&:movie_title).join(':')
collect(&:movie_title)
is the same as collect { |asset| asset.movie_title }
, which returns an Array of the movie titles. join(':')
creates a String with the values from the Array separated by :
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With