# How to simulate a column that do not exists in a model

**URL:** <https://www.ruby-forum.com/t/how-to-simulate-a-column-that-do-not-exists-in-a-model/194192>\
**Category:** Rails\
**Created:** [August 31, 2010, 8:49pm UTC](https://www.ruby-forum.com/t/how-to-simulate-a-column-that-do-not-exists-in-a-model/194192 "2010-08-31T20:49:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucas\_franceschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lucas\_franceschi](https://www.ruby-forum.com/u/lucas_franceschi)\
**Post date:** [August 31, 2010, 8:49pm UTC](https://www.ruby-forum.com/t/how-to-simulate-a-column-that-do-not-exists-in-a-model/194192/1 "2010-08-31T20:49:51Z")

</div>

Hello there…

what i am trying to do, is to consider a new column in my model, a  
column  
that do not exist in the database, and will not be created.

imagine, for instance, this case:

I have a table that store a number and a string, lets call it ‘words’

* * *

## words

## int times(PK) char name

imagine Word as an instance of the model, i will have access to these:  
Word.times  
Word.name

what if i wanted a new column, some like  
Word.complete

imagine that this column would store that ‘char’ for many times as is  
defined int the ‘times’ column…  
like this

Word.times = 3  
Word.name = a  
Word.complete = aaa

got it?

i wont store that data, but generate it based on the data that is  
stored.

any idea???

did any of you guys see this situation before???

–  
Lucas Franceschi  
Desenvolvedor  
SGI Sistemas  
(049) 9922-3360

---

<div class="post-metadata">

**Author:** ![lucas\_franceschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lucas\_franceschi](https://www.ruby-forum.com/u/lucas_franceschi)\
**Post date:** [August 31, 2010, 9:01pm UTC](https://www.ruby-forum.com/t/how-to-simulate-a-column-that-do-not-exists-in-a-model/194192/2 "2010-08-31T21:01:41Z")

</div>

lucas franceschi wrote:

> Word.complete = aaa

Word is the model class - it doesn’t store values.

If word is an instance, simply write:

word[:complete] = aaa

---

<div class="post-metadata">

**Author:** ![lucas\_franceschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lucas\_franceschi](https://www.ruby-forum.com/u/lucas_franceschi)\
**Post date:** [August 31, 2010, 9:08pm UTC](https://www.ruby-forum.com/t/how-to-simulate-a-column-that-do-not-exists-in-a-model/194192/3 "2010-08-31T21:08:35Z")

</div>

you didn’t get it…

the point is… i want

Word.complete

## to already be defined, so that in the controller i could use the “complete” column as if it was a real column…

Lucas Franceschi  
Desenvolvedor  
SGI Sistemas  
(049) 9922-3360

---

<div class="post-metadata">

**Author:** ![lucas\_franceschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lucas\_franceschi](https://www.ruby-forum.com/u/lucas_franceschi)\
**Post date:** [August 31, 2010, 9:13pm UTC](https://www.ruby-forum.com/t/how-to-simulate-a-column-that-do-not-exists-in-a-model/194192/4 "2010-08-31T21:13:17Z")

</div>

correction - you will need to check that both name and times\_repeated  
are not nil

def complete  
self.name \* self.times\_repeated if self.name && self.times\_repeated  
end

strictly speaking the “self.” isn’t needed either, but it does make  
the intention clearer…

---

<div class="post-metadata">

**Author:** ![lucas\_franceschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lucas\_franceschi](https://www.ruby-forum.com/u/lucas_franceschi)\
**Post date:** [August 31, 2010, 9:32pm UTC](https://www.ruby-forum.com/t/how-to-simulate-a-column-that-do-not-exists-in-a-model/194192/5 "2010-08-31T21:32:37Z")

</div>

just look up attr\_accessor method, if used within a model on a non  
existant field in the model, it will create a “pseudo” field. Do not  
forget to make accessible.

---

<div class="post-metadata">

**Author:** ![lucas\_franceschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lucas\_franceschi](https://www.ruby-forum.com/u/lucas_franceschi)\
**Post date:** [August 31, 2010, 9:09pm UTC](https://www.ruby-forum.com/t/how-to-simulate-a-column-that-do-not-exists-in-a-model/194192/6 "2010-08-31T21:09:46Z")

</div>

On 31 August 2010 19:48, lucas franceschi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Word.times = 3  
> Word.name = a  
> Word.complete = aaa  
> got it?  
> i wont store that data, but generate it based on the data that is stored.  
> any idea???  
> did any of you guys see this situation before???

once or twice…

what you’re after is a “method”…

(you may run into trouble if you try to call your column ‘times’, I’ll  
use ‘times\_repeated’ in this example)

# word.rb

def complete  
self.name \* self.times\_repeated if self.name  
end

I recommend going through some Ruby tutorials - the “Ruby for Rails”  
book has a good coverage of getting started through to intermediate  
use.
