# Belongs\_to / has\_many does not save

**URL:** <https://www.ruby-forum.com/t/belongs-to-has-many-does-not-save/159237>\
**Category:** Rails\
**Created:** [February 2, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-does-not-save/159237 "2009-02-02T17:45:58Z")\
**Posts on this page:** 5\
**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:** [February 2, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-does-not-save/159237/1 "2009-02-02T17:45:58Z")

</div>

Hi

I’m following pretty closely the following the Depot Application from  
the ‘Agile Web D. with Rails’ (3rd edition), but I run into  
deep trouble. I have:

class Product \< ActiveRecord::Base  
has\_many :charge\_items  
end

class Transaction \< ActiveRecord::Base  
belongs\_to :user  
has\_many :charge\_items  
end

class ChargeItem \< ActiveRecord::Base  
belongs\_to :transaction  
belongs\_to :product  
end

Running script/console, I try the following:  
Loading development environment (Rails 2.2.2)

> > p = Product.find(:first)  
> > =\> #\<Product id: 1, name: “50 Clicks”, description: “50 Clicks”,  
> > valid\_from: “2009-01-20 09:37:00”, valid\_until: “2010-01-20 09:37:00”,  
> > number\_of\_clicks: 50, price: #BigDecimal:7f4458231588,‘0.2E2’,9(18),  
> > created\_at: “2009-01-20 09:38:52”, updated\_at: “2009-01-20 09:38:52”\>
> 
> > t = Transaction.find(:first)  
> > =\> #\<Transaction id: 43, user\_id: 6, created\_at: “2009-02-02  
> > 09:07:24”, updated\_at: “2009-02-02 09:07:24”\>
> 
> > c = ChargeItem.new(:transaction =\> t, :product =\> p, :quantity =\> 1, :total\_price =\> 29.95)  
> > =\> #\<ChargeItem id: nil, transaction\_id: 43, product\_id: 1, quantity:  
> > 1, total\_price: #BigDecimal:7f44581e70c8,‘0.2995E2’,18(18),  
> > created\_at: nil, updated\_at: nil\>
> 
> > c.save  
> > =\> nil
> 
> >

I.e., the charge\_item never gets saved.

any hint or idea is extremely welcome.  
nd

---

<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:** [February 2, 2009, 6:43pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-does-not-save/159237/2 "2009-02-02T18:43:33Z")

</div>

On 2 Feb 2009, at 16:44, removed\_email\_address@domain.invalid wrote:

> Hi
> 
> I’m following pretty closely the following the Depot Application from  
> the ‘Agile Web D. with Rails’ (3rd edition), but I run into  
> deep trouble. I have:

Before 2.3 having an association called transaction is a bad bad idea.

Fred

---

<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:** [February 3, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-does-not-save/159237/3 "2009-02-03T14:32:41Z")

</div>

I ‘renamed’ the ‘Transaction’ model to ‘Order’ and voila: It works!

thanks!  
nd

On Feb 2, 6:42 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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:** [February 3, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/belongs-to-has-many-does-not-save/159237/4 "2009-02-03T20:20:42Z")

</div>

i know this seems obvious, but are you sure the table exists? also, are  
created\_at or updated\_at required columns in your schema? if so, they  
are  
nil and need to be set before you save.

On Mon, Feb 2, 2009 at 8:44 AM, removed\_email\_address@domain.invalid  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> end
> 
> > > c = ChargeItem.new(:transaction =\> t, :product =\> p, :quantity =\> 1,  
> > > any hint or idea is extremely welcome.  
> > > nd

–  
Available for Ruby on Rails development opportunities

---

<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:** [February 4, 2009, 12:43am UTC](https://www.ruby-forum.com/t/belongs-to-has-many-does-not-save/159237/5 "2009-02-04T00:43:49Z")

</div>

You can’t be following it THAT closely. Transaction is reserved in  
active record for database transactions

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)
