# Rails Object in DB is Invalid

**URL:** <https://www.ruby-forum.com/t/rails-object-in-db-is-invalid/180006>\
**Category:** Deployment\
**Created:** [December 9, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/rails-object-in-db-is-invalid/180006 "2009-12-09T16:14:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [December 9, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/rails-object-in-db-is-invalid/180006/1 "2009-12-09T16:14:36Z")

</div>

I have a model named Tickets that being saved to the database even when  
invalid. This is stopping me from using validations to help prevent  
duplicate data being saved to the DB. In script/console

> > Ticket.last.valid?  
> > False  
> > Ticket.first.valid?  
> > False

If i try to see what errors are associated with this invalid object

> > Ticket.last.errors.each{|attr,msg| puts “#{attr} - #{msg}\n” }  
> > {}

So does anyone know how its possible to save an invalid object to the  
database, and how can i find what is making the object invalid?

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [December 18, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/rails-object-in-db-is-invalid/180006/2 "2009-12-18T17:36:44Z")

</div>

I encountered a problem like this before and it turned out to be due to  
a callback which was returning false. Unbeknownst to me, if a callback  
returns false then the whole chain halts. Do you have any callbacks  
that fire before validation, and if so could any of them be returning  
false?

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [December 18, 2009, 8:08pm UTC](https://www.ruby-forum.com/t/rails-object-in-db-is-invalid/180006/3 "2009-12-18T20:08:31Z")

</div>

Max W. wrote:

> I encountered a problem like this before and it turned out to be due to  
> a callback which was returning false. Unbeknownst to me, if a callback  
> returns false then the whole chain halts. Do you have any callbacks  
> that fire before validation, and if so could any of them be returning  
> false?

Hmm…i only have a after\_create callback. Here is my ticket.rb model

[http://pastie.org/749098](http://pastie.org/749098)

Do any major problems jump out at you ?

Even stranger, i turned on debugger level logging, and i only see one  
insertion into the database, yet was getting two database entries (but  
again only sometimes).

When i tried writing to a file to help trace down the problem, i haven’t  
been able to reproduce the issue since: [http://pastie.org/749104](http://pastie.org/749104)

I call the create like this:

Ticket.find(:first, :conditions =\> {:from\_email =\> from\_email, :to\_email  
=\> to\_email, :body =\> message})||Ticket.create(:subject =\> subject,  
:from\_email =\> from\_email, :to\_email =\> to\_email, :body =\> message)

Any Ideas?

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [December 18, 2009, 10:17pm UTC](https://www.ruby-forum.com/t/rails-object-in-db-is-invalid/180006/4 "2009-12-18T22:17:42Z")

</div>

Thanks for the catch on the attr\_accessible. We actually figured out  
that it was causing errors, I was wasn’t checking the error correctly.

> <https://stackoverflow.com/questions/1874569/rails-object-in-database-is-invalid>

You are correct

“validates\_uniqueness\_of :to\_email, :scope =\> [:body, :from\_email]”

This does throw an error on to\_email only if the body and from\_email are  
also not unique. And this is the error i get when i check the errors on  
the duplicate objects in the database

I have tested this validation using rspec, and it behaves correctly…

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [December 18, 2009, 9:58pm UTC](https://www.ruby-forum.com/t/rails-object-in-db-is-invalid/180006/5 "2009-12-18T21:58:14Z")

</div>

the whole create/after\_create situation isn’t relevant to the question  
really. The question is ‘how come something fails validation but has no  
errors’.

You’ve got a complicated kind of situation going on with the  
attr\_accessibles which i don’t really understand. I can see that you’ve  
got :to\_email in there twice which is wrong but probably not the source  
of this particular problem.

I don’t really understand what this is for:

validates\_uniqueness\_of :to\_email, :scope =\> [:body, :from\_email]

You’re saying that the combination of :to\_email, :body and :from\_email  
has to be unique? Maybe that’s the problem but you’d expect to get an  
error raised if it wasn’t a unique combination.

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [December 18, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/rails-object-in-db-is-invalid/180006/6 "2009-12-18T23:50:00Z")

</div>

err, so is the mystery solved now or what? Is the original problem  
still persisting? I noticed something that is confusing the issue:

> > Ticket.last.valid?  
> > False  
> > Ticket.last.errors.each{|attr,msg| puts “#{attr} - #{msg}\n” }

Here, you load a record out of the db with Ticket.last, then call valid  
on it. Then, you load it out of the db AGAIN, from scratch, with  
Ticket.last again. So, it doesn’t have any errors because the current  
object hasn’t had .valid called on it: it’s only after calling .valid  
that it would have anything in .errors. Anything loaded fresh from the  
db will never have any errors.

So, instantiate an object and do all of your stuff with that. BTW, i  
usually call errors.full\_messages to see the errors. eg

ticket = Ticket.last  
ticket.valid?  
ticket.errors.full\_messages

Try that.
