Belongs_to / has_many does not save

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

On 2 Feb 2009, at 16:44, [email protected] 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

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

thanks!
nd

On Feb 2, 6:42 pm, Frederick C. [email protected]

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, [email protected]
[email protected]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

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

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/