Reputation: 12605
I have an activerecord model, Connection, that has inherited models such as SSH, S3, Local, etc. I have differing presence validations across those child models.
In my view, I have a "type" dropdown for connection type, which shows/hides different fields depending upon the chosen type. Upon save, the controller saves the params as a Connection type model.
The problem is that, seeing as the save method is run on an instance of the parent (Connection) class, the relevant child validations aren't checked, despite "type" being set to the necessary value.
I could get around this with a switch that saves a new instance of the model dependent upon the value of "type", but somehow that seems inelegant. ...So, is there a more elegant way of ensuring that, when my Connection model is saved with a value of (let's say) 'SSH' for type, the 'SSH' validations run on that model?
Upvotes: 0
Views: 302
Reputation: 18110
You might try the becomes
method
instance.becomes(OtherObject)
It returns an instance of the specified 'klass' with the attributes of the current record.
http://apidock.com/rails/ActiveRecord/Persistence/becomes
A little more elegant than the switch.
Another thought, call the correct controller - dynamically change the url of the form.
Upvotes: 1