Forum: Ruby on Rails ActiveRecord has_many :finder_sql

C7ba795494edd8280f3c5147af053b67?d=identicon&s=25 Cédric Clément (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 => 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?
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2012-05-03 15:48
(Received via mailing list)
On Wednesday, 2 May 2012 10:58:49 UTC-4, Ruby-Forum.com 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
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.