# Getting the current Action in the Model

**URL:** <https://www.ruby-forum.com/t/getting-the-current-action-in-the-model/51842>\
**Category:** Rails\
**Created:** [January 8, 2006, 5:49am UTC](https://www.ruby-forum.com/t/getting-the-current-action-in-the-model/51842 "2006-01-08T05:49:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [January 8, 2006, 5:49am UTC](https://www.ruby-forum.com/t/getting-the-current-action-in-the-model/51842/1 "2006-01-08T05:49:54Z")

</div>

Is it possible to get the current Controller’s action from within the  
Model? I have a situation where I want a piece of validation to run on  
edits, but not on new creates.

Thanks,

- Mark

---

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [January 8, 2006, 6:33am UTC](https://www.ruby-forum.com/t/getting-the-current-action-in-the-model/51842/2 "2006-01-08T06:33:04Z")

</div>

On Sun, Jan 08, 2006 at 05:49:54AM +0100, Mark H. wrote:

> Is it possible to get the current Controller’s action from within the  
> Model? I have a situation where I want a piece of validation to run on  
> edits, but not on new creates.

This functionality is build into the validation framework. By default  
validations are run always, but you can pass the :on option with either  
:update or :create as its value.

validate\_acceptance\_of :terms\_of\_service, :on =\> :create

Aside from validations, active record objects also have a new\_record?  
method.

t = Thing.new  
t.new\_record? # =\> true  
t.save  
t.new\_record? # =\> false  
Thing.find(t.id).new\_record? # =\> false

marcel

---

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [January 8, 2006, 9:50am UTC](https://www.ruby-forum.com/t/getting-the-current-action-in-the-model/51842/3 "2006-01-08T09:50:34Z")

</div>

On 1/7/06, Duane J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > - Mark  
> > @glazed  
> > if @donut.save  
> > …  
> > end  
> > end
> 
> def create  
> @donut.glazed = true  
> if @donut.save  
> …  
> end  
> end

Goddamn it, now you made me hungry. ☹

---

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [January 8, 2006, 6:20am UTC](https://www.ruby-forum.com/t/getting-the-current-action-in-the-model/51842/4 "2006-01-08T06:20:59Z")

</div>

On Jan 7, 2006, at 9:49 PM, Mark H. wrote:

> Is it possible to get the current Controller’s action from within the  
> Model? I have a situation where I want a piece of validation to  
> run on  
> edits, but not on new creates.
> 
> Thanks,
> 
> - Mark

You could hack things up to work that way, but there may be a better  
way: use conditional validations.

For example:

class Donut \< ActiveRecord::Base  
attr\_accessor :glazed

def glazed?  
@glazed  
end

validates\_numericality\_of :holes, :if =\> :glazed?  
end

Then in your controller:

def update  
@donut.glazed = false  
if @donut.save  
…  
end  
end

def create  
@donut.glazed = true  
if @donut.save  
…  
end  
end

This approach keeps the control on the Controller side, rather than  
requiring your Model to know about controller names and actions.

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)
