ruby - Loading validations from a table -
i need load model validations table , validate model. e.g. have database table called validations, has rows :
validation_action validation_condition ---------------- -------------------- validates_presence_of if answer_name name validates_format_of if answer_type date
in model want:
class model < activerecord::base load validation_actions , lambda {if validation_condition true}
ok more detail:
i creating app taking surveys. storing questions in table , answers in table. need store validation each answer in question table , validate each answer before accept it. can query validation each question , run in controller want in model instead cleaner.
so 2 models :
questions -> table questions sas code , details questions answers -> table answers stores answers foreign key questions.
i want validate input in answers model depending upon conditions defined in questions database table.
please let me know if more detail needed?
if understand correctly, have questions have many associated answers, , have associated validationactions. when answer being saved, want run validations against based on evaluating code stored in validationactions belonging it's associated question object.
this seems bad idea. not validations conditional on state of associated object (the question), sort of thing happen, you're doing amounts executing code contents of database. 1 of things makes web developers twitchy - security issue obvious reason (that if can edit database's fields, can cause arbitrary code run on system), it's questionable practice outside (i'm having trouble articulating reason more convincingly 'code code , data data , never twain shall meet', there is).
all said, should possible write custom validator as explained here handle this. doubtless inefficient, might prone breakage, , security hole unless it's written carefully. it's possible.
Comments
Post a Comment