Re: Link table logic

Peter De Berdt
2007-03-13T11:57:18Z

> How is this link represented in the rails model?
>
> For example, can I say:
>
> class User < ActiveRecord::Base
>   has_many :created_events
>   has_many :subscribed_events
>   has_many :events, :through => :created_events, :subscribed_events
>
> or something to that effect? Also, how would User.events work in this
> case? The logic doesn''t seem quite correct!
>
> Have I went about my database design in the wrong way?
>
> As always, any help is greatly appreciated - I hope I have made myself
> clear!

Untested, but fairly sure it will work:

Fetch the events for both relationships, then just add them:

def events
    cr_ev=created_events.events
    sb_ev=subscribed_events.events
    total = cr_ev + sb_ev
    total.uniq
end


Best regards

Peter De Berdt