Forum: Ruby on Rails belongs_to / has_many does not save

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2009-02-02 18:45
(Received via mailing list)
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
Frederick C. (Guest)
on 2009-02-02 19:43
(Received via mailing list)
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
(Guest)
on 2009-02-03 15:32
(Received via mailing list)
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>
Alan B. (Guest)
on 2009-02-03 21:20
(Received via mailing list)
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>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
Julian L. (Guest)
on 2009-02-04 01:43
(Received via mailing list)
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/
This topic is locked and can not be replied to.