bachmann-HHQVGFfPtShyDzI6CaY1VQ@public.gmane.org
2009-Feb-02 16:44 UTC
belongs_to / has_many does not save
Hi I''m following pretty closely the following the Depot Application from the ''Agile Web Development 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2 Feb 2009, at 16:44, bachmann-HHQVGFfPtShyDzI6CaY1VQ@public.gmane.org wrote:> > Hi > > I''m following pretty closely the following the Depot Application from > the ''Agile Web Development 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> 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bachmann-HHQVGFfPtShyDzI6CaY1VQ@public.gmane.org
2009-Feb-03 13:32 UTC
Re: belongs_to / has_many does not save
I ''renamed'' the ''Transaction'' model to ''Order'' and voila: It works! thanks! nd On Feb 2, 6:42 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 2 Feb 2009, at 16:44, bachm...-HHQVGFfPtShyDzI6CaY1VQ@public.gmane.org wrote: > > > > > Hi > > > I''m following pretty closely the following the Depot Application from > > the ''Agile Web Development 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 > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, bachmann-HHQVGFfPtShyDzI6CaY1VQ@public.gmane.org <bachmann-HHQVGFfPtShyDzI6CaY1VQ@public.gmane.org>wrote:> > Hi > > I''m following pretty closely the following the Depot Application from > the ''Agile Web Development 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 > > >-- Available for Ruby on Rails development opportunities --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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/ On 04/02/2009, at 6:19 AM, Alan Brown <stretch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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, bachmann-HHQVGFfPtShyDzI6CaY1VQ@public.gmane.org <bachmann-HHQVGFfPtShyDzI6CaY1VQ@public.gmane.org > > wrote: > > Hi > > I''m following pretty closely the following the Depot Application from > the ''Agile Web Development 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 > > > > > -- > Available for Ruby on Rails development opportunities > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---