# Create without triggering callbacks?

**URL:** <https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321>\
**Category:** Rails\
**Created:** [June 30, 2009, 5:14pm UTC](https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321 "2009-06-30T17:14:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 30, 2009, 5:14pm UTC](https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321/1 "2009-06-30T17:14:56Z")

</div>

Is there a way to do ModelName.create without triggering the various  
callbacks that would normally be triggered? I’m in an rspec test and  
want to save a model without worrying about the callback stuff.

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 30, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321/2 "2009-06-30T17:28:10Z")

</div>

Max W. wrote:

> Is there a way to do ModelName.create without triggering the various  
> callbacks that would normally be triggered?

Yes; check the AR docs. I believe save is the function you’re looking  
for.

> I’m in an rspec test and  
> want to save a model without worrying about the callback stuff.

Why? If you’re testing your app, you should have the callbacks work  
just like in the real thing.

If you’re just trying to cobble together some test data, consider using  
factories, probably with Machinist.

> thanks  
> max

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 30, 2009, 5:33pm UTC](https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321/3 "2009-06-30T17:33:24Z")

</div>

Marnen Laibow-Koser wrote:

> Max W. wrote:
> 
> > Is there a way to do ModelName.create without triggering the various  
> > callbacks that would normally be triggered?
> 
> Yes; check the AR docs. I believe save is the function you’re looking  
> for  
> Thanks for your advice. I have access to the AR docs, but doing new  
> then save triggers the same callbacks as calling create.

> > I’m in an rspec test and  
> > want to save a model without worrying about the callback stuff.
> 
> Why? If you’re testing your app, you should have the callbacks work  
> just like in the real thing.  
> But i’m not testing the model in question, it’s just an associated model  
> for the model i am testing. The callbacks are not relevant to these  
> tests at all.
> 
> If you’re just trying to cobble together some test data, consider using  
> factories, probably with Machinist.  
> Yeah, i know, i have used those before, it just seems like overkill for  
> this case.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 30, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321/4 "2009-06-30T18:52:18Z")

</div>

Max W. wrote:  
[…]

> > for  
> > Thanks for your advice. I have access to the AR docs, but doing new  
> > then save triggers the same callbacks as calling create.

Then maybe it’s something else, or maybe I’m thinking of just  
bypassing validatons…sorry to have steered you wrong!

> > > I’m in an rspec test and  
> > > want to save a model without worrying about the callback stuff.
> > 
> > Why? If you’re testing your app, you should have the callbacks work  
> > just like in the real thing.  
> > But i’m not testing the model in question, it’s just an associated model  
> > for the model i am testing. The callbacks are not relevant to these  
> > tests at all.

That makes some sense.

> > If you’re just trying to cobble together some test data, consider using  
> > factories, probably with Machinist.  
> > Yeah, i know, i have used those before, it just seems like overkill for  
> > this case.

It’s not overkill. It’s the exact situation for which factories are  
the perfect solution. If you’ve got your blueprints set up right, all  
you need is one more .make call.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 30, 2009, 7:11pm UTC](https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321/5 "2009-06-30T19:11:14Z")

</div>

Marnen Laibow-Koser wrote:

> > > If you’re just trying to cobble together some test data, consider using  
> > > factories, probably with Machinist.  
> > > Yeah, i know, i have used those before, it just seems like overkill for  
> > > this case.
> 
> It’s not overkill. It’s the exact situation for which factories are  
> the perfect solution. If you’ve got your blueprints set up right, all  
> you need is one more .make call.

Yeah…that was in a different project though 🙂 I know it’s wrong, but  
i just ended up stubbing the methods called by the callbacks.

cheers  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 30, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321/6 "2009-06-30T22:09:50Z")

</div>

Max W. wrote:  
[…]

> I know it’s wrong,

Then don’t do it. That way lies Bad Code.

> but  
> i just ended up stubbing the methods called by the callbacks.

That’s probably OK, although I’d tend to make life easier and use  
mock\_model.

> cheers  
> max

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 1, 2009, 10:15am UTC](https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321/7 "2009-07-01T10:15:52Z")

</div>

Andrew T. wrote:

> This plugin helps avoid call backs:  
> [GitHub - cjbottaro/without\_callbacks: Temporarily disable ActiveRecord callbacks.](http://github.com/cjbottaro/without_callbacks/tree/master)

Thanks!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 30, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/create-without-triggering-callbacks/171321/8 "2009-06-30T20:43:45Z")

</div>

On Tue, Jun 30, 2009 at 7:11 PM, Max  
Williams[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > you need is one more .make call.
> 
> Yeah…that was in a different project though 🙂 I know it’s wrong, but  
> i just ended up stubbing the methods called by the callbacks.
> 
> cheers  
> max

This plugin helps avoid call backs:

> **[GitHub - cjbottaro/without\_callbacks: Temporarily disable ActiveRecord...](https://github.com/cjbottaro/without_callbacks/tree/master)**
>
> master

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)

[http://MyMvelope.com](http://MyMvelope.com) - The SIMPLE way to manage your savings
