Validates_presence_of of polymophic fields

My models looks like:

class Relationship < ActiveRecord::Base
belongs_to :link_type
belongs_to :parent, :polymorphic => true
belongs_to :child, :polymorphic => true

validates_presence_of :link_type_id

validates_presence_of :parent_id

validates_presence_of :parent_type

validates_presence_of :child_id

validates_presence_of :child_type

end

class Person < ActiveRecord::Base
has_many :parents, :as => :parent, :class_name => “Relationship”
has_many :children, :as => :child, :class_name => “Relationship”
end

class Company < ActiveRecord::Base
has_many :parents, :as => :parent, :class_name => “Relationship”
has_many :children, :as => :child, :class_name => “Relationship”
end

The four validates_presence_of lines that are commented out fail when I
do a save. I’m doing this in my controller:

  relationship = Relationship.new(:link_type => link_type)
  @person.children << relationship
  relationship.parent = company

When I do the save, the fields are set properly in the database. Is
this a bug or am I not doing something right?

Thank you for your help

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