Saturday, 15 May 2010

ruby on rails - ActiveRecord: Validating inherited classes via parent -



ruby on rails - ActiveRecord: Validating inherited classes via parent -

i have activerecord model, connection, has inherited models such ssh, s3, local, etc. have differing presence validations across kid models.

in view, have "type" dropdown connection type, shows/hides different fields depending upon chosen type. upon save, controller saves params connection type model.

the problem that, seeing save method run on instance of parent (connection) class, relevant kid validations aren't checked, despite "type" beingness set necessary value.

i around switch saves new instance of model dependent upon value of "type", somehow seems inelegant. ...so, there more elegant way of ensuring that, when connection model saved value of (let's say) 'ssh' type, 'ssh' validations run on model?

you might seek becomes method

instance.becomes(otherobject)

it returns instance of specified 'klass' attributes of current record.

http://apidock.com/rails/activerecord/persistence/becomes

a little more elegant switch.

another thought, phone call right controller - dynamically alter url of form.

ruby-on-rails validation activerecord

No comments:

Post a Comment