ruby on rails - Is there a way to concatenate two fields, using Activerecord? WITHOUT using Virtual Attributes -


i'd join 2 fields together, preferably space in between. know can done @ model level, cannot predict fields , tables i'll mixing together, can't create virtual attribute this.

the following oversimplificated example of i'm trying do.

tables:

# product (id: integer, name: string, variety: string) # location (id: integer, city: string) 

this give me relation of locations, along product variety:

p=product.joins(:location).group(:location_id, :variety).pluck(:city, :variety) 

now need join location name , variety name together; that'd city location , variety product.

this 1 example, there're many more combinations i'd using cannot predict. i'd rather avoid creating virtual attributes this, it'd incredibly complex add attribute each possible combination, , creating own function withing affected models bit simple.

why can't join results after plucking? so:

p = product.joins(:location).group(:location_id, :variety).     pluck(:city, :variety).map{|e| e.join(' ')} 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -