# Activerecord's virtual attribute in find's array

**URL:** <https://www.ruby-forum.com/t/activerecords-virtual-attribute-in-finds-array/138482>\
**Category:** Rails\
**Created:** [May 23, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/activerecords-virtual-attribute-in-finds-array/138482 "2008-05-23T14:59:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [May 23, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/activerecords-virtual-attribute-in-finds-array/138482/1 "2008-05-23T14:59:32Z")

</div>

hi guys

i have a product model with many columns like  
cat1,cat2,cat3,name,desc,price,qty etc.

along with that i have a virtual attribute call full\_name in model

attr\_accessor :full\_name

def full\_name  
cat1+cat2+cat3+name #something like this  
end

when i do Product.find(:all)  
in the product array the product’s hash object does not have a attrubute  
with the name full\_name.

although i know if i write prod.full\_name it will work  
i want that attribute to be in the hash as a key-value pair  
as i want to convert the whole array to json.

what is the simplest way of doing this.

i had tried this but did not work

def after\_find  
full\_name  
end

thanks  
jags

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [May 23, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/activerecords-virtual-attribute-in-finds-array/138482/2 "2008-05-23T15:11:15Z")

</div>

On 23 May 2008, at 13:59, Jags R. wrote:

> although i know if i write prod.full\_name it will work  
> i want that attribute to be in the hash as a key-value pair  
> as i want to convert the whole array to json.

to\_json takes a :methods parameter where you can list methods to call  
whose result should be in the json eg

products.to\_json :methods =\> :[full\_name]

Fred
