I have a membership list based on the above. However, I allow for
prospective members to sign up with the reference (id) of a referer
(parent). So I am getting an id from ActiveView that I need to
validate and then slot the new member in as a child to the referer. I
managed to get the validation done to check that a member with the
supplied id does indeed exist, by adding the following to
validations.rb :
validates_each(attr_names, configuration) do |record, attr_name,
value|
record.errors.add(attr_name, configuration[:message]) unless
Member.find(:first, :conditions => [“id = ?”, value.to_i])
end
end
end
end
end
Note the find, using find(id) would have raised an exception that I
don’t want to deal with here. Then, in the model I just have:
<rails_code>
validates_id_exists :associate_reference
Now for my question: I cannot really do the textbook
parent.children.create(), since I need to allow
Rails to do its magic with validation of all the other fields on the
form (email, psw etc). So I opted for the following (attaching the
umbilical cord AFTER the save of the new record):
It works, but I have a feeling there must be a more elegant way of
doing this. Any words from the wise?