# Create! should return an Active Record object no matter what

**URL:** <https://www.ruby-forum.com/t/create-should-return-an-active-record-object-no-matter-what/99999>\
**Category:** Rails\
**Created:** [July 26, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/create-should-return-an-active-record-object-no-matter-what/99999 "2006-07-26T16:19:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [July 26, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/create-should-return-an-active-record-object-no-matter-what/99999/1 "2006-07-26T16:19:39Z")

</div>

I just using the bang versions of save and create after seeing Rick  
(technoweenie) use it in his code. I like it much better.

I have a problem with the create! method. It doesn’t return an Active  
Record object if it fails. That means you can’t display the errors on  
the object. Whats the point than?

Here is a broken example that should work.

def create  
@product = Product.create! params[:product]  
redirect\_to :action =\> “show”, :id =\> @product.id  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘new’  
end

If it fails @products is nil. You can’t show any errors.

Here is my work around.

def create  
@product = Product.create params[:product]  
@product.save!  
redirect\_to :action =\> “show”, :id =\> @product.id  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘new’  
end

The normal create is nice enough to keep @product around to display the  
errors.

This is how it is documented and supposed to be.

I’m going to try to “monkey patch” to make create! return an Active  
Record object.

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [July 26, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/create-should-return-an-active-record-object-no-matter-what/99999/2 "2006-07-26T16:22:58Z")

</div>

I agree with you.

I can’t see the purpose of a create! at all. You didn’t have an instance  
in the first place, so how can it be updating it?

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [July 26, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/create-should-return-an-active-record-object-no-matter-what/99999/3 "2006-07-26T16:28:52Z")

</div>

I’m going work on a patch.

Please don’t submit one. I want to be a “Rails Contributor”. 🙂

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [July 27, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/create-should-return-an-active-record-object-no-matter-what/99999/4 "2006-07-27T17:58:28Z")

</div>

On Wed, Jul 26, 2006, Josh P. wrote:

> I have a problem with the create! method. It doesn’t return an Active  
> Record object if it fails. That means you can’t display the errors on  
> the object. Whats the point than?

As you discovered lower down, it should be raising an exception. This  
is normal for Ruby, where methods shouldn’t return anything, as they’re  
meant to do thing in-place.

I agree with you and Dr. Nic, it doesn’t make any sense to me to have  
save!  
and create! methods, but I guess some people have found utility in them.

Ben

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [July 26, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/create-should-return-an-active-record-object-no-matter-what/99999/5 "2006-07-26T16:34:54Z")

</div>

Um… Maybe it just needs a documentation patch to clear things up.

Found in changelog

Add Model.create! to match existing model.save! method. When save!  
raises RecordInvalid, you can catch the exception, retrieve the invalid  
record (invalid\_exception.record), and see its errors  
(invalid\_exception.record.errors).
