I have nested attributes for a Rails model and the associations validations are failing for some reason. I am not useing accepts_nested_attributes_for, but I am doing something very similar.
class Project < ActiveRecord::Base
has_many :project_attributes
def name
project_attributes.find_by_name("name")
end
def name=(val)
attribute = project_attributes.find_by_name("name")
if attribute
attribute.value = val
else
project_attributes.build(:name=>"name", :value=>val)
end
end
end
class ProjectAttribute < ActiveRecord::Base
belongs_to :project
validates_presence_of :name
validates_uniqueness_of :name, :scope => :project_id
validates_presence_of :project_id, :unless => lambda {|attribute| attribute.project.try(:valid?)}
validates_associated :project
end
This is a contrived example, but similar to what I am trying to do. I've taken a look at what accepts_nested_attributes_for does and it uses the build method, which I thought would associate the built attribute with the project.
I also looked at http://stackoverflow.com/questions/935650/acceptsnestedattributesfor-child-association-validation-failing which gave me the validates_presence_of :unless=>valid?
Any ideas on how to get this to work?