On Wed, Jan 25, 2012 at 11:31 AM, Bala TS <[email protected]> wrote:

> Hai!
>
> One-to-one
>
> use has_one in the base, and belongs_to in the associated model.
>
> class Parent < ActiveRecord::Base
>  has_one :child
> end
>
> class Child < ActiveRecord::Base
>  belongs_to :parent    # foreign key - parent_id
> end
>

Thank you for the feedback. I tried and this is the result:

class Child < ActiveRecord::Base
  has_one :parent
  validates :name, :presence => true
end

class Parent < ActiveRecord::Base
  has_one :child
end

$ rails c
Loading development environment (Rails 3.2.0)
1.9.3-p0 :001 > p = Parent.new
 => #<Parent id: nil, name: nil, created_at: nil, updated_at: nil>
1.9.3-p0 :002 > c1 = p.build_child
   (0.2ms)  BEGIN
   (0.2ms)  COMMIT
 => #<Child id: nil, name: nil, parent_id: nil, created_at: nil,
updated_at: nil>
1.9.3-p0 :003 > p.valid?
 => true
1.9.3-p0 :004 > c1.valid?
 => false
1.9.3-p0 :005 > p.save
   (0.2ms)  BEGIN
  SQL (5.8ms)  INSERT INTO "parents" ("created_at", "name", "updated_at")
VALUES ($1, $2, $3) RETURNING "id"  [["created_at", Wed, 25 Jan 2012
11:58:00 UTC +00:00], ["name", nil], ["updated_at", Wed, 25 Jan 2012
11:58:00 UTC +00:00]]
   (24.2ms)  COMMIT
 => true

The line above reports "success" (true), while the child is not auto-saved


Now trying to implicitly save a non-valid child with a straight assignment.

1.9.3-p0 :022 > p.child = Child.new
   (0.2ms)  BEGIN
   (0.4ms)  UPDATE "children" SET "parent_id" = NULL, "updated_at" =
'2012-01-25 12:08:36.168342' WHERE "children"."id" = 4
   (0.2ms)  ROLLBACK
ActiveRecord::RecordNotSaved: Failed to save the new associated child.
    from 
/home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/has_one_association.rb:23:in
`block in replace'
    from 
/home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in
`transaction'
    from 
/home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/transactions.rb:208:in
`transaction'
    from 
/home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/has_one_association.rb:11:in
`replace'
    from 
/home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/singular_association.rb:17:in
`writer'
    from 
/home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/builder/association.rb:51:in
`block in define_writers'

I would have expected this behavior (an exception for p.save!) that is
happening for an explicit save of an associated child, to also occur on
an "auto-save" of an associated child.

Thanks,

Peter

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to