Hello, I''ve come across an issue that I''m sure is a bug when using after_create with a has_one association, but I''m not 100% certain if I''m missing something. This is pretty much exactly the code I''m using. Two simple classes (Account and Contact) and I create the contact after I create an account (via after_create). I''m not passing in a "name" field which the contact requires via validates_presence_of, so the contact should not get saved, but it in fact does. class Account < ActiveRecord::Base has_one :contact def after_create create_contact end end class Contact < ActiveRecord::Base belongs_to :account validates_presence_of :name end The test... class AccountTest < ActiveSupport::TestCase test "this ain''t right" do a = Account.create p a.contact p a.new_record? end end produces... #<Contact id: 996332878, name: nil, account_id: 1, created_at: "2009-05-25 05:40:40", updated_at: "2009-05-25 05:40:40"> false Is this expected behaviour? Thanks, Andrew -- Posted via http://www.ruby-forum.com/.
Oh, forgot to mention... rails 2.3.2 -- Posted via http://www.ruby-forum.com/.
Perhaps you should file a ticket? Rails.lighthouseapp.com Blog: http://random8.zenunit.com/ Twitter: http://twitter.com/random8r Learn: http://sensei.zenunit.com/ New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy process! Check it out now! On 25/05/2009, at 3:46 PM, Andrew Kaspick <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > wrote:> > Hello, > > I''ve come across an issue that I''m sure is a bug when using > after_create > with a has_one association, but I''m not 100% certain if I''m missing > something. > > This is pretty much exactly the code I''m using. Two simple classes > (Account and Contact) and I create the contact after I create an > account > (via after_create). I''m not passing in a "name" field which the > contact > requires via validates_presence_of, so the contact should not get > saved, > but it in fact does. > > class Account < ActiveRecord::Base > has_one :contact > def after_create > create_contact > end > end > > class Contact < ActiveRecord::Base > belongs_to :account > validates_presence_of :name > end > > The test... > > class AccountTest < ActiveSupport::TestCase > test "this ain''t right" do > a = Account.create > p a.contact > p a.new_record? > end > end > > produces... > > #<Contact id: 996332878, name: nil, account_id: 1, created_at: > "2009-05-25 05:40:40", updated_at: "2009-05-25 05:40:40"> > false > > Is this expected behaviour? > > Thanks, > Andrew > -- > Posted via http://www.ruby-forum.com/. > > >
On 25/05/2009, at 3:46 PM, Andrew Kaspick wrote:> > Hello, > > I''ve come across an issue that I''m sure is a bug when using > after_create > with a has_one association, but I''m not 100% certain if I''m missing > something. >Perhaps you should file a bug? http://rails.lighthouseapp.com ---------------------------------------------- Blog: http://random8.zenunit.com/ Twitter: http://twitter.com/random8r Learn: http://sensei.zenunit.com/ Latest: How to use Unix basics at http://sensei.zenunit.com/
It''s not *correct* behavior, but it wasn''t caught before 2.3.2 went out the door. It''s been fixed in edge - more details at https://rails.lighthouseapp.com/projects/8994/tickets/2249 --Matt Jones On May 25, 1:46 am, Andrew Kaspick <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > I''ve come across an issue that I''m sure is a bug when using after_create > with a has_one association, but I''m not 100% certain if I''m missing > something. > > This is pretty much exactly the code I''m using. Two simple classes > (Account and Contact) and I create the contact after I create an account > (via after_create). I''m not passing in a "name" field which the contact > requires via validates_presence_of, so the contact should not get saved, > but it in fact does. > > class Account < ActiveRecord::Base > has_one :contact > def after_create > create_contact > end > end > > class Contact < ActiveRecord::Base > belongs_to :account > validates_presence_of :name > end > > The test... > > class AccountTest < ActiveSupport::TestCase > test "this ain''t right" do > a = Account.create > p a.contact > p a.new_record? > end > end > > produces... > > #<Contact id: 996332878, name: nil, account_id: 1, created_at: > "2009-05-25 05:40:40", updated_at: "2009-05-25 05:40:40"> > false > > Is this expected behaviour? > > Thanks, > Andrew > -- > Posted viahttp://www.ruby-forum.com/.