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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -