views:

43

answers:

2

Hello,

I'm so used to oracle where you can simply

concat(field1, ' ', field2)

but if I'm using activerecord to find the field1 and field2, and I need a space in between, how do I accomplish this?

Cheers for all your help

+2  A: 

I think virtual attributes are right up your alley. This Railscast explains them, with an example that looks just like your use case.

Jordan
+4  A: 

in your model:

def full_name
  [first_name, last_name].join(' ')
end
bobbywilson0
can this be used strictly for just displaying in a select tag?
no, it just returns a string of two concatenated fields, so anywhere you want to use it.
bobbywilson0
by chance, how do I actually write that if I have something like this, which is the first select which has the name and the second select which is the price... select "sales", "name", Sales.find(:all, :select => 'DISTINCT name', :order => 'price').collect{|x| [ x.name]}
you need to clarify the question in order for me to understand what you are asking
bobbywilson0
Well okay, I wanted to concat price and name together to form one select tag instead of the two I have already. I was wondering how they this ties with the example that you gave me. I'm new to ruby, but I've been spending a lot of time trying to get this working :S
In your model: class Post < ActiveRecord::Base def name_and_price [price, name].join(' ') end endIn your view:`collection_select(:post, :author_id, Product.all, :id, :name_with_price)`
bobbywilson0