开发者

How to make activerecord use a field generated on the fly by find_by_sql

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


@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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜