# Record is not saved to database

**URL:** <https://www.ruby-forum.com/t/record-is-not-saved-to-database/147568>\
**Category:** Rails\
**Created:** [September 3, 2008, 8:11am UTC](https://www.ruby-forum.com/t/record-is-not-saved-to-database/147568 "2008-09-03T08:11:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rasfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ba0ec/32.png) [@rasfast](https://www.ruby-forum.com/u/rasfast)\
**Post date:** [September 3, 2008, 8:11am UTC](https://www.ruby-forum.com/t/record-is-not-saved-to-database/147568/1 "2008-09-03T08:11:42Z")

</div>

Hi, all!

Maybe a noob question, sorry, but I’m really stuck with this.  
I have 2 models with has\_many and belongs\_to associations.  
class Transaction \< ActiveRecord::Base  
has\_many :bonuses, :class\_name =\> ‘TransactionBonus’  
end  
class TransactionBonus \< ActiveRecord::Base  
belongs\_to :transaction  
end

The problem is that I cannot save TransactionBonus record to the  
database - it’s simply doesn’t do anything!  
I have another models in my application, and all of them are saved  
just fine  
TransactionBonus is the only exception

Table structure matches exactly the model records:  
CREATE TABLE IF NOT EXISTS `transaction_bonuses` (  
`id` int(11) NOT NULL auto\_increment,  
`transaction_id` int(11) NOT NULL,  
`taken_bonus` float default NULL,  
`collected_bonus` float default NULL,  
`final_sum` float default NULL,  
`created_at` datetime default NULL,  
`updated_at` datetime default NULL,  
PRIMARY KEY (`id`)  
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO\_INCREMENT=1 ;

Here is my irb console output with test records:

> > transaction=Transaction.find(1)  
> > =\> #\<Transaction id: 1, card\_account\_id: 1, created\_at:  
> > “2008-09-01 13:48:10”, updated\_at: “2008-09-01 13:48:10”\>

> > transaction.bonuses  
> > =\> []

> > bonus=transaction.bonuses.create(:transaction\_id =\> 1, :final\_sum =\> 99)  
> > =\> #\<TransactionBonus id: nil, transaction\_id: 1, final\_sum: 99.0,  
> > created\_at: nil, updated\_at:  
> > nil\>

> > transaction.bonuses  
> > =\> [#\<TransactionBonus id: nil, transaction\_id: 1, final\_sum: 99.0,  
> > created\_at: nil, updated\_at:  
> > nil\>]

> > bonus.save  
> > =\> #\<Transaction id: 1, card\_account\_id: 1, created\_at:  
> > “2008-09-01 13:48:10”, updated\_at: “2008-09-01 13:48:10”\>

> > bonus  
> > =\> #\<TransactionBonus id: nil, transaction\_id: 1, final\_sum: 99.0,  
> > created\_at: nil, updated\_at:  
> > nil\>

After that I have tried to initialize TransactionBonus record  
directly, but no success:

> > bonus = TransactionBonus.create( :transaction\_id =\> 1 )  
> > =\> #\<TransactionBonus id: nil, transaction\_id: 1, final\_sum: nil,  
> > created\_at: nil, updated\_at: nil\>

> > bonus  
> > =\> #\<TransactionBonus id: nil, transaction\_id: 1, final\_sum: nil,  
> > created\_at: nil, updated\_at: nil\>

> > bonus.save  
> > =\> #\<Transaction id: 1, card\_holder\_id: 0, card\_account\_id: 1,  
> > created\_at: “2008-09-01 13:48:10”, updated\_at: “2008-09-01  
> > 13:48:10”\>

> > bonus  
> > =\> #\<TransactionBonus id: nil, transaction\_id: 1, final\_sum: nil,  
> > created\_at: nil, updated\_at: nil\>

Please, note id: nil for the bonus var, it means that the record is  
not saved to the database.

This is really mysterious for me…

---

<div class="post-metadata">

**Author:** ![rasfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ba0ec/32.png) [@rasfast](https://www.ruby-forum.com/u/rasfast)\
**Post date:** [September 3, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/record-is-not-saved-to-database/147568/2 "2008-09-03T17:06:02Z")

</div>

rasfast wrote:

> > > bonus = TransactionBonus.create( :transaction\_id =\> 1 )  
> > > =\> #\<TransactionBonus id: nil, transaction\_id: 1, final\_sum: nil,  
> > > created\_at: nil, updated\_at: nil\>

Try using create! to see if the problem is with validation or  
due to a false-returning pre-save callback.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![rasfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ba0ec/32.png) [@rasfast](https://www.ruby-forum.com/u/rasfast)\
**Post date:** [September 3, 2008, 8:13am UTC](https://www.ruby-forum.com/t/record-is-not-saved-to-database/147568/3 "2008-09-03T08:13:28Z")

</div>

On Sep 2, 8:51 pm, rasfast [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Don’t call your model transaction - you’re overriding some internal  
activerecord methods.

Fred
