Skip to content

Loading…

Validation message on association #717

Closed
samdebutcha opened this Issue · 7 comments

2 participants

@samdebutcha

class User < ActiveRecord::Base
has_and_belongs_to_many :roles
validates :roles, :presence => {:message => 'cannot save without role'}
end

Trying to save User without assigning role, shows no error validation message.

@bbenezech
Collaborator

I was not aware it is possible to validate presence of associateds through has_many. Does it work without RA? In console for example?

@samdebutcha

validates :roles, :presence => {:message => "Missing role?"}

Yes, it works in the console:

u = User.create(:userid => "5", :password => "mypasswd", :email => "xxx@xxx.com")
u.errors
ActiveModel::Errors:0x406ab40 @messages= OrderedHash
{:email2=>["съдържа невярна стойност"], :roles=>["Missing role?"]}

@bbenezech
Collaborator

Have a look there: https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/fields/association.rb#L84

Field::HasManyAssociation is inheriting from Field::Association and apparently using child_key for retrieving errors on its field. We should override this behavior in Field::HasManyAssociation and use association's name instead.
If it's supported by AM, then we should add a test too.

@samdebutcha

Yes, if override in Field::HasManyAssociation
def errors
bindings[:object].errors[name]
end
it shows the message: roles Missing role
Thank you. What happens now?

@bbenezech
Collaborator

Pull request? :D

@samdebutcha

Never pulled request before, never added test before.
Some guidance?

@samdebutcha

OK, I pulled request.

@samdebutcha samdebutcha closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.