Forum: Ruby on Rails finder_sql issue with has_many :through

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ryan W. (Guest)
on 2006-06-09 23:15
I have an inner join query that Action Record can't seem to pull off
conventionally, so I'm trying to use :finder_sql to utilize an sql query
that works beautifully in SQL. The problem is, when I try to call the
method, I get the following:

private method `gsub' called for #<Array:0x267de04>

Here is my has_many code:

has_many :mod_privileges,
         :finder_sql => ['SELECT assignments.* FROM assignments INNER
JOIN permissions ON assignments.publisher_id = permissions.publisher_id
WHERE permissions.user_id = #{current_user.id} AND (permissions.role_id
= 3 OR (permissions.role_id = 4 and permissions.assignment_id =
assignments.id))']

and a simple test bit I threw into the view:

<%= debug(current_user.mod_privileges)  %>

I was using has_many :through, but there seemed to be no way to do an
inner join on something other than the column referencing the main
select table.

Suggestions?
Ryan W. (Guest)
on 2006-06-10 00:14
Nevermind. I forgot the :class_name. This works:

  has_many :mod_privileges,
           :class_name => "Assignment",
           :finder_sql => "SELECT assignments.* FROM assignments INNER
JOIN permissions ON assignments.publisher_id = permissions.publisher_id
WHERE permissions.user_id = #{current_user.id} AND (permissions.role_id
= 3 OR (permissions.role_id = 4 and permissions.assignment_id =
assignments.id))"

However, now I'm getting:

uninitialized constant User

I'm using Acts_as_Authenticated which gives me access to the user via
current_user, but I'm assuming since this has_many method is IN the User
model it's not working?
This topic is locked and can not be replied to.