# Database error

**URL:** <https://discuss.rubyonrails.org/t/database-error/32752>\
**Category:** rubyonrails-talk\
**Created:** [August 4, 2008, 6:45am UTC](https://discuss.rubyonrails.org/t/database-error/32752 "2008-08-04T06:45:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 4, 2008, 6:45am UTC](https://discuss.rubyonrails.org/t/database-error/32752/1 "2008-08-04T06: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:** ![Hubert\_Lepicki1](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Hubert\_Lepicki1](https://discuss.rubyonrails.org/u/Hubert_Lepicki1)\
**Post date:** [August 4, 2008, 7:27am UTC](https://discuss.rubyonrails.org/t/database-error/32752/2 "2008-08-04T07:27:09Z")

</div>

HI,

> 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..

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 &nbsp;&nbsp;# we have email in database! do nothing! else &nbsp;&nbsp;# insert email in database end

Thi should do the trick.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 4, 2008, 7:52am UTC](https://discuss.rubyonrails.org/t/database-error/32752/3 "2008-08-04T07:52:08Z")

</div>

if Email.count(:conditions =\> ["message\_id = ?", 1234]) \> 0 &nbsp;&nbsp;# we have email in database! do nothing! else &nbsp;&nbsp;# 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:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 4, 2008, 8:22am UTC](https://discuss.rubyonrails.org/t/database-error/32752/4 "2008-08-04T08:22:09Z")

</div>

> HI,
> 
> 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:

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:** ![Hubert\_Lepicki1](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Hubert\_Lepicki1](https://discuss.rubyonrails.org/u/Hubert_Lepicki1)\
**Post date:** [August 4, 2008, 8:28am UTC](https://discuss.rubyonrails.org/t/database-error/32752/5 "2008-08-04T08:28:00Z")

</div>

HI,

> 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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 5, 2008, 2:15am UTC](https://discuss.rubyonrails.org/t/database-error/32752/6 "2008-08-05T02: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
