Change association methods behaviour

Hello there,

I am building an application where users are distributed among different
user groups (User has_many UserGroup). But there is a specific type of
User
(admin) that I would like to belong to ALL groups. I know I can add them
to
the group on the after_create or after_update method of UserGroup, but I
would like to do something different.

My question is:

Is it possible that user.user_groups return all groups if the user is an
admin, and the normal group list if he is not? Admin is a boolean field
of
User.

if you need more info, please let me know.

best,
Dante

finder_sql may be a way to do this, something like:

has_many :user_groups, :finder_sql => “SELECT DISTINCT user_groups.*
FROM user_groups #{'WHERE user_groups.user_id = ’ + id unless admin}”

On 30 May 2008, at 14:10, Thorsten M. wrote:

finder_sql may be a way to do this, something like:

has_many :user_groups, :finder_sql => “SELECT DISTINCT user_groups.*
FROM user_groups #{'WHERE user_groups.user_id = ’ + id unless admin}”

although that will have to be => ‘…’ so that this is interpolated at
the right point in time

Fred