ruby - Prevent Custom Validation Error on Association -


i have app list , email models many many association. want users able add many emails lists @ once, have function in controller split emails custom textfield in new list form "emails" array.

i want validation error if it's blank added custom validation on field in lists model:

class list < applicationrecord   has_and_belongs_to_many :emails #tried adding ", validates: false" here too, didn't work  #custom   attr_accessor :emails   validates :emails,     presence: true end 

it works great, except when try associate existing email list (in case user entered 1 in db), validation error. oddly, can prevent validation error when associating net new emails lists "(validation: false)" seen below, not when email exists already, if add save function , append "validation: false". part of controller in question is:

emails.each |email|   if email.where(email: email).blank?     db_email = email.new(         email: email     )     db_email.events << @event     db_email.lists << @list #adds association     if db_email.save(validate: false)       #nothing     else       #logic email errors       #redirect_to('/', {:flash => { :error => "'#{email}' not valid email adderss" }})     end   else     existing_email = email.where(email: email).take     existing_email.events << @event     existing_email.lists << @list #get validation error here   end end 

any advise on how validate field, prevent validations when associations created?


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -