validate :something, except: :create

79 views
Skip to first unread message

Benjamin B

unread,
Oct 14, 2015, 3:50:40 AM10/14/15
to Ruby on Rails: Core

Greeting,


Want to gather your :+1: and :-1: on an idea I had, before spending time working on a PR.


The idea is simple
The same way that ActiveModel::Validations::ClassMethods#validates accepts the option on to specify the contexts where this validation is active, I'm thinking to add the option except to exclude contexts where the validation is active.


Use case
Publication has many Page, AttachmentFile, Image, Comments etc...

All those model have their own set of validation that execute on create and on update. Few of them are quite expensive (such as triggering database access to verify uniqueness)

Publication can be published. Before publishing we run a specific set of validations with on: :publishing options. Those validation are not needed for the model to exist on the database, but needed for the publishing operation. But, we don't want to run again the expensive original sets of validation, we assume they are already valid anyhow because the record exists in the database.


How to do
Currently, this works perfectly:


with_options unless: Proc.new { |record| record.validation_context == :publishing } do |record|
  record.validates_presence_of :things
  record.validates_uniqueness_of :fields, scope: :publication_id
end

with_options on: :publishing do |record|
  record.validates_presence_of :pages
end
 

Suggestion
But it is not very intuitive and require to dive a bit into the source code to find about validation_context before being able to implement it.

So I would suggest to write:


with_options except: :publishing do |record|
  ...
end
 

Thoughts?

Benjamin B

unread,
Oct 27, 2015, 2:27:09 AM10/27/15
to Ruby on Rails: Core
Up
Any thoughts?

Allam Marcos Campanini Matsubara

unread,
Oct 27, 2015, 3:43:38 AM10/27/15
to rubyonra...@googlegroups.com
I wish something like that on ActiveRecord yesterday, so, definitely :+1:.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.



--
Allam Marcos Campanini Matsubara

skype: allam.matsubara
twitter: @allam_matsubara
Phone: +55 (41) 8847-8677

Jon Calhoun

unread,
Nov 20, 2015, 12:18:02 AM11/20/15
to Ruby on Rails: Core
I'd love to see this feature added to rails core, and thanks Benjamin for posting this workaround for now :D

Benjamin B

unread,
Nov 21, 2015, 3:50:37 PM11/21/15
to Ruby on Rails: Core
Reply all
Reply to author
Forward
0 new messages