Forum: Ruby on Rails ActiveRecord has_many :finder_sql

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C937f4ff15f58046d9f245bc9c69a02a?d=identicon&s=25 Cédric C. (schille)
on 2012-05-02 16:58
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 =>{%Q{SELECT * FROM v1_utilisateur}}

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

What am I doing wrong?
Matt Jones (Guest)
on 2012-05-03 15:48
(Received via mailing list)
On Wednesday, 2 May 2012 10:58:49 UTC-4, User wrote:
>   self.sequence_name = 'SQ1_UTILISATEUR'
> 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
it. You'll encounter weird bugs, *and* wind up with data out of sync
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
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
you want, adding a simple method to user rather than an association may
clearer. Specifying :foreign_key on an association with :finder_sql has
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
This topic is locked and can not be replied to.