Rails select and concatenate subset of columns -
i have following activerecord row:
#<location id: 1, name: "test center", description: nil, address: "some road along way", city: "porto", country: "pt", latitude: nil, longitude: nil, created_at: "2015-06-05 19:03:04", updated_at: "2015-06-05 19:03:04">
what need end this:
some road along way,porto,pt
which coma seperated list of address
, city
, country
.
i tried call select
, turn hash first, no avail, ideas?
there several solutions many variations this. here two:
1. put them in array , join it:
[location.address, location.city, location.country].join(',')
2. use string interpolation:
"#{location.address},#{location.city},#{location.country}"
you can wrap them in method in model or create rails helper.
in model:
class location < activerecord::base # other code here def description [address, city, country].join(',') end end
helper method:
def location_description(location) [location.address, location.city, location.country].join(',') end
Comments
Post a Comment