# SQL - int not inserted into database correctly

**URL:** <https://www.ruby-forum.com/t/sql-int-not-inserted-into-database-correctly/131516>\
**Category:** Rails\
**Created:** [March 10, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/sql-int-not-inserted-into-database-correctly/131516 "2008-03-10T22:08:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 10, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/sql-int-not-inserted-into-database-correctly/131516/1 "2008-03-10T22:08:04Z")

</div>

Hello  
I’m using writertopia authentication plugin. Looking at the Rails  
server output I see the following insert when I call  
current\_user.has\_role ‘moderator’ @group

SQL (0.000154) BEGIN  
Role Create (0.000424) INSERT INTO `roles` (`name`, `updated_at`,  
`authorizable_type`, `authorizable_id`, `created_at`)  
VALUES(‘moderator’, ‘2008-03-10 20:36:37’, ‘Group’, 23, ‘2008-03-10  
20:36:37’)  
SQL (0.000497) COMMIT

so far so good. I then check the database and instead of having  
roles.authorizable\_id = 23 it = NULL.

±—±----------±------------------±----------------  
±--------------------±--------------------+  
| id | name | authorizable\_type | authorizable\_id |  
created\_at | updated\_at |  
±—±----------±------------------±----------------  
±--------------------±--------------------+  
| 21 | moderator | Group | NULL | 2008-03-10  
20:36:37 | 2008-03-10 20:36:37 |  
±—±----------±------------------±----------------  
±--------------------±--------------------+

If I execute the SQL for the mysql commandline prompt I get the  
expected result and a non null authorizable\_id.

Any suggestions about how to go about fixing / debugging would be very  
appreciated.  
Cheers,  
Ewan

P.S I have the same problem using Ruby-based MySQL library that ships  
with Rails, as I have using the C-based MySQL library.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 11, 2008, 11:48am UTC](https://www.ruby-forum.com/t/sql-int-not-inserted-into-database-correctly/131516/2 "2008-03-11T11:48:39Z")

</div>

A new day and I’ve found the problem. Looking further down the Rails  
server output I discover I’m updating the roles table and inserting  
the null value, inadvertently. I was temporally changing the  
association on a model object not realising the changes were save  
automatically.
