# How to add virtual fields in model

**URL:** <https://www.ruby-forum.com/t/how-to-add-virtual-fields-in-model/52380>\
**Category:** Rails\
**Created:** [January 13, 2006, 1:12pm UTC](https://www.ruby-forum.com/t/how-to-add-virtual-fields-in-model/52380 "2006-01-13T13:12:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [January 13, 2006, 1:12pm UTC](https://www.ruby-forum.com/t/how-to-add-virtual-fields-in-model/52380/1 "2006-01-13T13:12:35Z")

</div>

I want to add virtual fields in a model.

table: field1 field2  
I want to add field3 which is field1\*2 + field2

I know tihs can be achieved in View, but I want it in model.  
Then I will pass this model to PagerRenderer to paginate.

How to achieve this?

thanks

---

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [January 13, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/how-to-add-virtual-fields-in-model/52380/2 "2006-01-13T13:37:05Z")

</div>

Superbiji wrote:

> I want to add virtual fields in a model.
> 
> table: field1 field2  
> I want to add field3 which is field1\*2 + field2
> 
> I know tihs can be achieved in View, but I want it in model.  
> Then I will pass this model to PagerRenderer to paginate.
> 
> How to achieve this?
> 
> thanks

In your model…

def field3  
field1 + field2  
end

---

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [January 13, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/how-to-add-virtual-fields-in-model/52380/3 "2006-01-13T13:38:30Z")

</div>

Something like the following should work just fine:

class YourModel  
…  
def field3  
field1 \* 2 + field2  
end  
end

As for rendering the list. It should paginate fine - but you might not  
be  
able to use the standard way of determining which columns will be  
rendered  
(I don’t think it will pick up your “virtual field”). Someone with more  
experience could chime in here.

Cheers,  
Pete.

---

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [January 13, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/how-to-add-virtual-fields-in-model/52380/4 "2006-01-13T13:41:32Z")

</div>

Kevin O. wrote:

> def field3  
> field1 + field2  
> end

@pages, @items = ctx.paginate tablename, opt

I’ve did that, but my field3 does not shown in @items

---

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [January 13, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/how-to-add-virtual-fields-in-model/52380/5 "2006-01-13T13:47:21Z")

</div>

Peter S. wrote:

> As for rendering the list. It should paginate fine - but you might not  
> be  
> able to use the standard way of determining which columns will be  
> rendered

My PagerRenderer is dumb renderer, which I will use for all my model. It  
iterates model’s columns using @items[0].class.content\_columns
