Wins Lin
2013-May-30 15:55 UTC
How to pass more than one argument to Rails validation hooks?
I don''t know how to pass many arguments to before_validation hook. I want to implement the hook on both create and update methods: before_validation(:on => [:create, :update]) { // do something } But this form of arguments :on => [:create, :update] throws an error: /active_support/callbacks.rb:403: syntax error, unexpected ''['', expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END ... (self.validation_context == :[:create, :update]) This one :on => :create, :update also doesn''t work: syntax error, unexpected '')'', expecting tASSOC before_validation(:on => :create, :update) { And API has only one example with one argument: before_validation(:on => :create) do self.number = number.gsub(/[^0-9]/, "") if attribute_present?("number") end How to pass multiple arguments? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0873dace5e98892073621b1422d27818%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Rafael
2013-May-30 16:03 UTC
Re: How to pass more than one argument to Rails validation hooks?
Per this: http://guides.rubyonrails.org/active_record_validations_callbacks.html#available-callbacks before_validation only occurs/runs on create and update try this: before_validation do |object_name| #some code end On Thursday, May 30, 2013 11:55:53 AM UTC-4, Ruby-Forum.com User wrote:> > I don''t know how to pass many arguments to before_validation hook. I > want to implement the hook on both create and update methods: > > before_validation(:on => [:create, :update]) { > // do something > } > But this form of arguments :on => [:create, :update] throws an error: > > /active_support/callbacks.rb:403: syntax error, unexpected ''['', > expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END > ... (self.validation_context == :[:create, :update]) > > > This one :on => :create, :update also doesn''t work: > > syntax error, unexpected '')'', expecting tASSOC > before_validation(:on => :create, :update) { > > > And API has only one example with one argument: > > before_validation(:on => :create) do > self.number = number.gsub(/[^0-9]/, "") if > attribute_present?("number") > end > > How to pass multiple arguments? > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a503a22f-2670-453f-8c8e-6b93faf28025%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Wins Lin
2013-May-30 16:14 UTC
Re: How to pass more than one argument to Rails validation hooks?
Rafael wrote in post #1110688:> Per this: >http://guides.rubyonrails.org/active_record_validations_callbacks.html#available-callbacks> > before_validation only occurs/runs on create and update >Thank you. I found an explanation. Just to pass :on => save will include both: class Person < ActiveRecord::Base # it will be possible to update email with a duplicated value validates :email, :uniqueness => true, :on => :create # it will be possible to create the record with a non-numerical age validates :age, :numericality => true, :on => :update # the default (validates on both create and update) validates :name, :presence => true, :on => :save end -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a01a262b328bad655c496b3a85946ee8%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.