Re: Associations - Better solution?

pepe
2009-07-14T00:05:24Z

Hi Fred,

Just [re]discovered about STI right after my answer to the first
reply. I made the necessary changes to the users table, tried it and
it worked quite fine when I fixed the kinks trying to access the value
with user.type. Once I figured that one out everything worked as
expected.

Thanks.

Pepe

On Jul 12, 4:35 pm, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> On Jul 12, 3:43 pm, pepe
<P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:
>
> > When a user is retrieved for access and functionality validations, I
> > re-retrieve the user through the specific user class as in:
> >   user = User.find(...)
> >   # The user is found and passes validations.
> >   # Now I re-retrieve it through the specific user class based in the
> > category value.
> >   user = user.category.capitalize.constantize.find(user.id) if user
>
> > From that moment on I have the user retrieved through its correct type
> > and my associations work wonderfully.
>
> If you use single table inheritance then User.find(...) would
> automatically return an instance of Tenant, Auditor or Admin as
> appropriate and you wouldn''t need to refetch it.
>
> Fred
>
>
>
> > Is there a better way of making this work?
>
> > Thanks.
>
> > Pepe