# Database error

**URL:** <https://www.ruby-forum.com/t/database-error/144908>\
**Category:** Rails\
**Created:** [August 4, 2008, 8:45am UTC](https://www.ruby-forum.com/t/database-error/144908 "2008-08-04T08:45:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [August 4, 2008, 8:45am UTC](https://www.ruby-forum.com/t/database-error/144908/1 "2008-08-04T08:45:51Z")

</div>

Hi i am new to ruby on rails. I am using RMail which is ruby mail to  
read the email that i fetch from. When the rmail read the email it will  
insert the data inside the email to the database which i have created in  
the first place. However i don’t wish to have duplicate data inside the  
database and i found out that every email have it own unique message-id.  
But i does not know how to validate the new email’s message-id with the  
one in the database whether there is the same data inside the database.

For example,

My new email the message-id is jKqhYh15424 but there is the same  
message-id inside the database and which i don’t wish to insert the new  
data into the database…

Help. Thanks.

Wawa

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [August 4, 2008, 9:27am UTC](https://www.ruby-forum.com/t/database-error/144908/2 "2008-08-04T09:27:59Z")

</div>

HI,

On 4 Sie, 08:45, Emmie W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My new email the message-id is jKqhYh15424 but there is the same  
> message-id inside the database and which i don’t wish to insert the new  
> data into the database…

Well, if you store emails for example in “Email” model (“emails”  
collumn in  
database), then you can easily find out if such email exists in your  
database  
by using ActiveRecord::count method. For example if message-id is  
stored in  
“message\_id” collumn:

if Email.count(:conditions =\> [“message\_id = ?”, 1234]) \> 0

# we have email in database! do nothing!

else

# insert email in database

end

Thi should do the trick.

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [August 4, 2008, 9:52am UTC](https://www.ruby-forum.com/t/database-error/144908/3 "2008-08-04T09:52:08Z")

</div>

if Email.count(:conditions =\> [“message\_id = ?”, 1234]) \> 0

# we have email in database! do nothing!

else

# insert email in database

end

Is the if [“message\_id = ?”, 1234]) \> 0 hard-coded. As i will have  
different kind of emails been send everyday. So there will be quite a  
lot of different message\_id i got to validate with. Help. Thanks.

Wawa

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [August 4, 2008, 10:28am UTC](https://www.ruby-forum.com/t/database-error/144908/4 "2008-08-04T10:28:54Z")

</div>

HI,

On Aug 4, 10:22 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Or slightly more concisely ( and faster in cases where there could be  
> more than one matching row)
> 
> Email.exists? :conditions =\>…

True, true!

> > if Email.count(:conditions =\> [“message\_id = ?”, 1234]) \> 0
> > 
> > # we have email in database! do nothing!
> > 
> > else
> > 
> > # insert email in database
> > 
> > end

You don’t have to hardcoder 1234, just use variable there or  
params[:message\_id] or whatever you like instead of 1234.

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [August 4, 2008, 10:23am UTC](https://www.ruby-forum.com/t/database-error/144908/5 "2008-08-04T10:23:38Z")

</div>

On 4 Aug 2008, at 08:27, Hubert ??picki [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> stored in  
> “message\_id” collumn:

Or slightly more concisely ( and faster in cases where there could be  
more than one matching row)

Email.exists? :conditions =\>…

Fred

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [August 5, 2008, 4:15am UTC](https://www.ruby-forum.com/t/database-error/144908/6 "2008-08-05T04:15:18Z")

</div>

Hi newbie is here. Ok thanks anyway. I got another error.

I got this sql query.

mysql = my.query(“select messageID from maps where  
DateTime=’#{messageId}’”)

the messageID is the database columns name while the messageId is i read  
from the messageId = message.header[‘Message-id’]. whish is a RMail  
method of reading the email header.

When i do print mysql, it print out #Mysql::Result:0xb7f49310. Is this  
a binary data. So how to i convert into a string. Help. Thanks.

Wawa
