i have a taskmanager type of project that i am working on, where i need to separate joins on the same two tables. i need one join for users that are assigned to the task, and another join for who has read the task, so they can tell which ones are new. any suggestions on the best way to accomplish this? thanks -- Posted via http://www.ruby-forum.com/.
Hi Josh, I think you might be able to model this with has_many :through either one Model (e.g. UserTaskRelation with two flags: ''read'' and ''assigned'' in it) or two Models ''Reading'', ''Assignation'' User has_many :tasks, :through => :reading User has_many :tasks, :through => :assignation with the second approach you are able with the standard created_at, updated_at to tell when a user did a reading or has been assigned to a task. Cheers, Jan -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060807/50269c33/attachment.html
Josh Kieschnick
2006-Aug-07 13:37 UTC
[Rails] Re: multiple habtm joins on the same two tables
Jan Prill wrote:> Hi Josh, > > I think you might be able to model this with has_many :through either > one > Model (e.g. UserTaskRelation with two flags: ''read'' and ''assigned'' in > it) or > two Models ''Reading'', ''Assignation'' > User has_many :tasks, :through => :reading > User has_many :tasks, :through => :assignation > > with the second approach you are able with the standard created_at, > updated_at to tell when a user did a reading or has been assigned to a > task. > > Cheers, > Janthanks for the help what about the database schema? should the link tables be reading_tasks and assignation_tasks for those conventions to work? -- Posted via http://www.ruby-forum.com/.
Hi Josh, no for the through association you don''t need a solely join table of the kind of comments_posts. Instead you''ve got a full featured model (and its underlying table like ''readings'') wich might grow with your application and it''s growing needs and is even able to be handled in the great way of the so called restful or crud controllers. Do a little research on ''has_many :through'' as well as restful and you''ll get the idea. HABTM has it''s use cases but a full ''through-model'' has so many advantages especially for cases like yours. Cheers, Jan On 8/7/06, Josh Kieschnick <jjkiesch@gmail.com> wrote:> > Jan Prill wrote: > > Hi Josh, > > > > I think you might be able to model this with has_many :through either > > one > > Model (e.g. UserTaskRelation with two flags: ''read'' and ''assigned'' in > > it) or > > two Models ''Reading'', ''Assignation'' > > User has_many :tasks, :through => :reading > > User has_many :tasks, :through => :assignation > > > > with the second approach you are able with the standard created_at, > > updated_at to tell when a user did a reading or has been assigned to a > > task. > > > > Cheers, > > Jan > > thanks for the help > > what about the database schema? should the link tables be reading_tasks > and assignation_tasks for those conventions to work? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060807/5f9d340b/attachment-0001.html
Jan Prill wrote:> Hi Josh, > > I think you might be able to model this with has_many :through either > one > Model (e.g. UserTaskRelation with two flags: ''read'' and ''assigned'' in > it) or > two Models ''Reading'', ''Assignation'' > User has_many :tasks, :through => :reading > User has_many :tasks, :through => :assignation > > with the second approach you are able with the standard created_at, > updated_at to tell when a user did a reading or has been assigned to a > task.This is sort of backwards. I think you want to have one join model and two associations going through it. User: has_many :assignments has_many :assigned_tasks, :through => :assignments, :class_name => "Task" has_many :read_tasks, :through => :assignments, :class_name => "Task" You can read more about this approach here: http://blog.hasmanythrough.com/articles/2006/05/06/through_gets_uniq -- Josh Susser http://blog.hasmanythrough.com/ -- Posted via http://www.ruby-forum.com/.