Reno
Reno

Reputation: 2982

Concatenating two fields in a collect

Rails 2.3.5

I'm not having any luck searching for an answer on this. I know I could just write out a manual sql statement with a concat in it, but I thought I'd ask:

To load a select, I'm running a query of shift records. I'm trying to make the value in the select be shift date followed by a space and then the shift name. I can't figure out the syntax for doing a concat of two fields in a collect. The Ruby docs make it looks like plus signs and double quotes should work in a collect but everything I try gets a "expected numeric" error from Rails.

  @shift_list =  [a find query].collect{|s| [s.shift_date + " " + s.shift_name, s.id]}

Thanks for any help - much appreciated.

Upvotes: 1

Views: 1991

Answers (1)

mu is too short
mu is too short

Reputation: 434615

Hard to say without knowing what s is going to be or what type s.shift_date and s.shift_name are but maybe you're looking for this:

collect{|s| ["#{s.shift_date} #{s.shift_name}", s.id]}

That is pretty much the same as:

collect{|s| [s.shift_date.to_s + ' ' + s.shift_name.to_s, s.id]}

but less noisy.

Upvotes: 9

Related Questions