ActiveRecord has_many :finder_sql

I'm in trouble with an pl/sql query. My user ActiveRecord::Base class has a specific query. When I call this query, I've an error "TypeError no _dump_data is defined for class Proc".

There is the ActiveRecord::Base class

class Utilisateur < ActiveRecord::Base   self.table_name = 'v1_utilisateur'   self.primary_key = 'utilid'   self.sequence_name = 'SQ1_UTILISATEUR'

  has_many :util_service,           :foreign_key => 'utilid',           :conditions => "modcode='SERVICE'",           :order => 'sercode'   has_many :direction,             :class_name => "Utilisateur",             :finder_sql => Proc.new{%Q{SELECT * FROM v1_utilisateur}} end

I use this instruction to call the query session[:direction] = @user.direction

What am I doing wrong?

Couple things:

  • don’t put ActiveRecord models in the session, if you can possibly avoid it. You’ll encounter weird bugs, and wind up with data out of sync with the DB.

  • @user.direction isn’t even an AR model, it’s an association proxy. @user.direction.to_a will get you an array of real models, if that’s what you want.

  • :finder_sql needs to specify the entire SQL query including whatever foreign-key-like expressions are needed, so the association as declared above will return the same records for every user. If this is really what you want, adding a simple method to user rather than an association may be clearer. Specifying :foreign_key on an association with :finder_sql has no effect, as the :foreign_key option is ignored.

  • has_manys are typically spelled with the plural version; has_many :directions rather than has_many :direction. Not an actual bug, but a serious annoyance for anybody reading your code later.

–Matt Jones