# Setting a message on a model -without- errors.add\_to\_base

**URL:** <https://www.ruby-forum.com/t/setting-a-message-on-a-model-without-errors-add-to-base/187825>\
**Category:** Rails\
**Created:** [April 30, 2010, 12:22am UTC](https://www.ruby-forum.com/t/setting-a-message-on-a-model-without-errors-add-to-base/187825 "2010-04-30T00:22:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phoenix\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@Phoenix\_R](https://www.ruby-forum.com/u/Phoenix_R)\
**Post date:** [April 30, 2010, 12:22am UTC](https://www.ruby-forum.com/t/setting-a-message-on-a-model-without-errors-add-to-base/187825/1 "2010-04-30T00:22:55Z")

</div>

I’m trying my best to follow the skinny controller/fat model concept,  
and I’ve got a series of callbacks (that I’m about to move into  
observers) running on a few models. Based on what these callbacks do  
to the data (it’s implementing business rules), I’d like to add some  
type of message on the state of the model _without_ invalidating it.

For example:

class Order \< ActiveRecord::Base  
after\_save :do\_something\_cool

private  
def do\_something\_cool  
# manipulates the order object in some way based on business rules  
errors.add\_to\_base(“Message about what we did to your order to  
match said business rules”)  
end  
end

I’d prefer to do this without calling errors.add\_to\_base, because it  
isn’t an error. Furthermore, I don’t even think that works because  
it’s being called -after- the model is saved (so far, even though the  
callback in my case is running, it doesn’t appear to be adding  
anything to the base errors object anyway).

Obviously I can’t set flash[:notice] inside the model as it’s owned by  
the controller, and I really don’t want to do some insane hacking to  
incorporate elements of the controller into the model (that would  
really make this spaghetti code and kill performance!).

The only thing I’ve thought about so far is setting a virtual  
attribute of some kind, but I’d like to see if anybody has a better  
idea before I jump straight to that.

Any thoughts on the matter? Thanks for any input you can provide!

---

<div class="post-metadata">

**Author:** ![Phoenix\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@Phoenix\_R](https://www.ruby-forum.com/u/Phoenix_R)\
**Post date:** [April 30, 2010, 11:38am UTC](https://www.ruby-forum.com/t/setting-a-message-on-a-model-without-errors-add-to-base/187825/2 "2010-04-30T11:38:57Z")

</div>

On Apr 30, 12:21 am, Phoenix R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The only thing I’ve thought about so far is setting a virtual  
> attribute of some kind, but I’d like to see if anybody has a better  
> idea before I jump straight to that.

I think a non-DB (“virtual”) attribute is a good idea.  
Simply declare the attributes with attr\_accessor.

class Order \< AR::B  
attr\_accessor :my\_attribute  
end

This will add the initialization of the instance variable and create  
getters and setters.
