Thanks a lot. I'll play with it. :)

Pepe

On Jul 12, 11:14 am, "Älphä Blüë" <[email protected]>
wrote:
> > Users:
> >   id
> >   category # can be either 'ADMIN', 'AUDITOR' or 'TENANT'
>
> > Audits:
> >   id
> >   auditor_id
> >   tenant_id
>
> Looking at this original Table format you have two foreign_keys in
> auditor and tenant.
>
> auditor_id matches user.id
> tenant_id matches user.id
> admin_id matches user.id
>
> > class User < ActiveRecord::Base
> >   has_many :audits
> > end
>
> You can design your User table and use self-referential associations
> within a given model.
>
> class User < ActiveRecord::Base
>   has_many  :audits
>   has_many  :auditors,  :through => :audits
>   has_many  :tenants,   :through => :audits
> end
>
> > class Audit < ActiveRecord::Base
> >   belongs_to :user # !!! This does not work even using :class_name,
> > etc. !!!
> > end
>
> class Audit < ActiveRecord::Base
>   belongs_to  :user
>   belongs_to  :auditor, :class_name => "User"
>   belongs_to  :tenant, :class_name => "User"
> end
>
> Using this format you can define any method in your Audit model with
> something similar to:
>
> :joins => [:user, :auditor, :tenant]
>
> .. which will join those self-referential tables together
>
> And then in your controller if you use:
>
> @audits = Audit.all :include => [:user, :auditor, :tenant]
>
> In your view you can do:
>
> @audits.each do |audit|
>   audit.auditor.category
>   audit.tenant.category
>   audit.user.category
>
> Notice that the above example basically will find out the category for
> each listing based on the foreign_key ids assigned through the
> associations.
>
> I only show this to you because it allows you to use multiple
> foreign_keys within one table and can help you rethink the design of
> your tables/models depending on the use.
>
> I hope this gives you a little bit of an idea..
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to