# Getter and Setter with non DB property

**URL:** <https://www.ruby-forum.com/t/getter-and-setter-with-non-db-property/187143>\
**Category:** Rails\
**Created:** [April 18, 2010, 1:49pm UTC](https://www.ruby-forum.com/t/getter-and-setter-with-non-db-property/187143 "2010-04-18T13:49:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [April 18, 2010, 1:49pm UTC](https://www.ruby-forum.com/t/getter-and-setter-with-non-db-property/187143/1 "2010-04-18T13:49:52Z")

</div>

Hi,

I can’t find how do i created a getter and setter property for a non db  
property. what i have done isnt working, i have a cnt mass assign error.

Here is what i’ve done:  
attr\_accessor :creator\_is\_participant

def creator\_is\_participant  
@participe  
end

def creator\_is\_participant=(participe)  
if participe == true  
@participe = true  
else  
@participe = false  
end  
end

Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [April 18, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/getter-and-setter-with-non-db-property/187143/2 "2010-04-18T14:55:27Z")

</div>

On Apr 18, 12:49 pm, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I can’t find how do i created a getter and setter property for a non db  
> property. what i have done isnt working, i have a cnt mass assign error.

The mass assignment error is probably not related to this (at least  
not what you have shown)

> Here is what i’ve done:  
> attr\_accessor :creator\_is\_participant

What this does is create a getter and setter method of that name that  
stores its data in @creator\_is\_participant - if you’re going to  
redefine both of those methods then you don’t need that

> def creator\_is\_participant=(participe)  
> if participe == true  
> @participe = true  
> else  
> @participe = false  
> end  
> end

This may have unintended consequences further down the line because it  
puts you at odds with what the ruby world considers to be true/false:  
nil and false are false, everything else has the truth value true

Fred
