Working with dynamic fields

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
..

#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.. ?

Thanks,
Diego

activemodel (3.2.12) lib/active_model/attribute_methods.rb:407:in
`method_missing'
activerecord (3.2.12) lib/active_record/attribute_methods.rb:149:in
`method_missing'
activesupport (3.2.12) lib/active_support/callbacks.rb:693:in
`_run__3928182467864996920__validate__2158410757724998838__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
`__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in
`_run_validate_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in
`run_callbacks'
activemodel (3.2.12) lib/active_model/validations.rb:228:in
`run_validations!'
activemodel (3.2.12) lib/active_model/validations/callbacks.rb:53:in
`block in run_validations!'
activesupport (3.2.12) lib/active_support/callbacks.rb:436:in
`_run__3928182467864996920__validation__2158410757724998838__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
`__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in
`_run_validation_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in
`run_callbacks'
activemodel (3.2.12) lib/active_model/validations/callbacks.rb:53:in
`run_validations!'
activemodel (3.2.12) lib/active_model/validations.rb:195:in `valid?'
activerecord (3.2.12) lib/active_record/validations.rb:69:in `valid?'
activerecord (3.2.12) lib/active_record/validations.rb:77:in
`perform_validations'
activerecord (3.2.12) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.12) lib/active_record/attribute_methods/dirty.rb:22:in
`save'
activerecord (3.2.12) lib/active_record/transactions.rb:259:in `block (2
levels) in save'
activerecord (3.2.12) lib/active_record/transactions.rb:313:in `block in
with_transaction_returning_status'
activerecord (3.2.12)
lib/active_record/connection_adapters/abstract/database_statements.rb:192:in
`transaction'
activerecord (3.2.12) lib/active_record/transactions.rb:208:in
`transaction'
newrelic_rpm (3.6.5.130) lib/new_relic/agent/method_tracer.rb:518:in
`block in transaction_with_trace_ActiveRecord_self_name_transaction'
newrelic_rpm (3.6.5.130) lib/new_relic/agent/method_tracer.rb:268:in
`trace_execution_scoped'
newrelic_rpm (3.6.5.130) lib/new_relic/agent/method_tracer.rb:513:in
`transaction_with_trace_ActiveRecord_self_name_transaction'
activerecord (3.2.12) lib/active_record/transactions.rb:311:in
`with_transaction_returning_status'
activerecord (3.2.12) lib/active_record/transactions.rb:259:in `block in
save'
activerecord (3.2.12) lib/active_record/transactions.rb:270:in
`rollback_active_record_state!'
activerecord (3.2.12) lib/active_record/transactions.rb:258:in `save'
inherited_resources (1.4.0)
lib/inherited_resources/base_helpers.rb:66:in `create_resource'
inherited_resources (1.4.0) lib/inherited_resources/actions.rb:33:in
`create'
app/controllers/users_controller.rb:41:in `create'
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in
`send_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:167:in
`process_action'
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in
`process_action'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in
process_action'
activesupport (3.2.12) lib/active_support/callbacks.rb:469:in
`_run__3196014063568727894__process_action__914905966018578265__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
`__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in
`_run_process_action_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in
`run_callbacks'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:17:in
`process_action'
actionpack (3.2.12) lib/action_controller/metal/rescue.rb:29:in
`process_action'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:30:in
`block in process_action'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `block
in instrument'
activesupport (3.2.12)
lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in
`instrument'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:29:in
`process_action'
actionpack (3.2.12) lib/action_controller/metal/params_wrapper.rb:207:in
`process_action'
activerecord (3.2.12)
lib/active_record/railties/controller_runtime.rb:18:in `process_action'
newrelic_rpm (3.6.5.130)
lib/new_relic/agent/instrumentation/rails3/action_controller.rb:38:in
`block in process_action'
newrelic_rpm (3.6.5.130)
lib/new_relic/agent/instrumentation/controller_instrumentation.rb:318:in
`perform_action_with_newrelic_trace'
newrelic_rpm (3.6.5.130)
lib/new_relic/agent/instrumentation/rails3/action_controller.rb:37:in
`process_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.12) lib/abstract_controller/rendering.rb:45:in `process'
rack-mini-profiler (0.1.28)
Ruby/lib/mini_profiler/profiling_methods.rb:71:in `block in
profile_method'
actionpack (3.2.12) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.12) lib/action_controller/metal/rack_delegation.rb:14:in
`dispatch'
actionpack (3.2.12) lib/action_controller/metal.rb:246:in `block in
action'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in
`call'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in
`dispatch'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:36:in
`call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:601:in
`call'
newrelic_rpm (3.6.5.130) lib/new_relic/rack/error_collector.rb:12:in
`call'
newrelic_rpm (3.6.5.130) lib/new_relic/rack/agent_hooks.rb:22:in `call'
newrelic_rpm (3.6.5.130) lib/new_relic/rack/browser_monitoring.rb:16:in
`call'
newrelic_rpm (3.6.5.130) lib/new_relic/rack/developer_mode.rb:28:in
`call'
apartment (0.21.0) lib/apartment/reloader.rb:19:in `call'
apartment (0.21.0) lib/apartment/elevators/generic.rb:19:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
actionpack (3.2.12)
lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.12)
lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/flash.rb:242:in
`call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/cookies.rb:341:in
`call'
activerecord (3.2.12) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.12)
lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in
`call'
actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:28:in
`block in call'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
`_run__840412673517469496__call__2158410757724998838__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in
`__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in
`_run_call_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in
`run_callbacks'
actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:27:in
`call'
actionpack (3.2.12) lib/action_dispatch/middleware/reloader.rb:65:in
`call'
actionpack (3.2.12) lib/action_dispatch/middleware/remote_ip.rb:31:in
`call'
actionpack (3.2.12)
lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.12)
lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.12) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.12) lib/rails/rack/logger.rb:18:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/request_id.rb:22:in
`call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.12)
lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/static.rb:62:in
`call'
rack-cache (1.2) lib/rack/cache/context.rb:136:in `forward'
rack-cache (1.2) lib/rack/cache/context.rb:143:in `pass'
rack-cache (1.2) lib/rack/cache/context.rb:155:in `invalidate'
rack-cache (1.2) lib/rack/cache/context.rb:71:in `call!'
rack-cache (1.2) lib/rack/cache/context.rb:51:in `call'
rack-mini-profiler (0.1.28) Ruby/lib/mini_profiler/profiler.rb:209:in
`call'
railties (3.2.12) lib/rails/engine.rb:479:in `call'
railties (3.2.12) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.12) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.5.0) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.0) lib/thin/connection.rb:79:in `catch'
thin (1.5.0) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.0) lib/thin/connection.rb:54:in `process'
thin (1.5.0) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.5.0) lib/thin/backends/base.rb:63:in `start'
thin (1.5.0) lib/thin/server.rb:159:in `start'
rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.5) lib/rack/server.rb:268:in `start'
railties (3.2.12) lib/rails/commands/server.rb:70:in `start'
railties (3.2.12) lib/rails/commands.rb:55:in `block in <top
(required)>'
railties (3.2.12) lib/rails/commands.rb:50:in `tap'
railties (3.2.12) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'

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. :slight_smile:

#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

Thanks Matt Jones!
I will create my validations!

Thanks,
Diego