Belongs_to validating presence of foreign key

I’ve recently read
and I seem to be experiencing conflicting behavior with Rails 1.2.3

Parent Table:
id Integer
name String

Child Table:
id Integer
name String
parent_id Integer

class Parent < ActiveRecord::Base
has_many :children

class Child < ActiveRecord::Base
belongs_to :parent
validates_presence_of :parent_id

So from reading the link above, as well as the warning on
I figured this would work.

parent = => “Dad”)
child = => “Son”)!

However I consistently get “ActiveRecord::RecordInvalid: Validation
failed: Children is invalid”

This behavior is exhibited with both “validates_presence_of :parent”
and “:parent_id”

However, if I remove the “validates_presence_of :parent_id” both
elements are saved and the Child has been saved with the parent id

Am I mis-reading something, or did this work in previous versions?

Running on Intel Mac, against both SQLite3 and MySQL.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs