You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So, for anyone having the same issue, I found a workaround, you can define a before_remove in the has_and_belongs_to_many association and raise an exception that then you can catch in ActiveAdmin.
For this it would be
class Client < ActiveRecord::Base
has_and_belongs_to_many :agents, before_remove: :check_something
validates :agents, presence: true
end
def check_something(agent)
if self.agents.size == 1
raise ActiveModel::MissingAttributeError.new 'something'
end
end
Expected behavior
When updating a record with changes to its children that make the record fail validation, no changes are saved to the database.
Actual behavior
The invalid association change is persisted, and then the user gets redirected to a form displaying a validation error.
How to reproduce
This seems to be due to the behavior of
assign_attributes
(as detailed in http://guides.rubyonrails.org/association_basics.html#has-and-belongs-to-many-association-reference). Maybeupdate_resource
should check if anids
field is present, and if so usecollection.build
for that?The text was updated successfully, but these errors were encountered: