0

I'm using find_by_sql with Activerecord which I generate another field there that doesn't in the original table as a combination of different fields like:

select (field1 + field2) as new_field_name

If I try to access the newly generated field like:

@user.new_field_name

I get nothing! How do you suggest I should approach this problem

Tam
  • 11,872
  • 19
  • 69
  • 119

1 Answers1

2

@user = select (field1 + field2) as new_field_name

This will return array although you get only one record.itearte a loop over @user

for user in @user
   puts user.new_field_name ###this should return a sum of field1 & field2 ###
end
OR
if you want 1st record then

@user[0].new_field_name

Salil
  • 46,566
  • 21
  • 122
  • 156
  • But the stored values are integers! – Tam Mar 29 '10 at 05:58
  • try this @user[0].new_field_name because you get the array not a single object if it works you have to put limit or itearte loop over @user.please let me know if it works ...... – Salil Mar 29 '10 at 06:09
  • Thanks...One reason it didn't work work initially with my is that I had attr_accessor new_field_name in the model. It worked after removing it – Tam Mar 29 '10 at 06:31