Ruby on Rails Thursday, August 15, 2013



On Wednesday, 14 August 2013 16:30:38 UTC-4, Ruby-Forum.com User wrote:
Hi,
I'm developing a app with dynamic field using hstore ( postgres ).
The app works great, but now, i'm trying to implement validations.
my code is:

#model
..
def initialize_custom_fields
  class_eval do
    validates field.f, :presence => true
  end
end

This is not a good idea - in development, this *might* work, but in production this will permanently attach a new validation to the class every time it is called. Don't do that. :)

 
..

#user controller
def create
  user = User.new
  user.initialize_custom_fields
  user.attributes = params[:user]
  user.save
end

The first time I press "save" button, the validations works as well, but
the second one i'm getting this error:

undefined local variable or method `_callback_before_170045' for
#<User:0x007f80f9990558>

Anyone have a idea for solution this error.. ?



If you really want different validations every time, it would be cleaner to use something like `validate :some_method_name` and then write explicit code to check the fields you're expecting. 

--Matt Jones 

--
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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/65ff57ff-e750-477b-a4b2-e06d5cfb66cc%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment