On Wed, Jan 25, 2012 at 1:15 PM, Peter Vandenabeele
<pe...@vandenabeele.com>wrote:

> On Wed, Jan 25, 2012 at 11:31 AM, Bala TS <li...@ruby-forum.com> 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
>

Sorry, that should have been Child belongs_to :parent ...

But, the results are the same. Trying again:

.../app/models$ cat *
class Child < ActiveRecord::Base
  belongs_to :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.7ms)  INSERT INTO "parents" ("created_at", "name", "updated_at")
VALUES ($1, $2, $3) RETURNING "id"  [["created_at", Wed, 25 Jan 2012
12:18:32 UTC +00:00], ["name", nil], ["updated_at", Wed, 25 Jan 2012
12:18:32 UTC +00:00]]
   (21.9ms)  COMMIT
 => true
1.9.3-p0 :006 > p.child = Child.new
   (0.2ms)  BEGIN
   (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'

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

Reply via email to