Hey there, I''m trying to do a really complex join with ActiveRecord and am coming up pretty short so far. The structure is like: user id name etc. task name ect. task_assignments from_user_id to_user_id task_id I''m not sure how to model it correctly. I checked out the has_many :through post about doing self referential joins, but that''s for getting a user from a user. I want to get the actual task_assignments from a user: @user.task_assignments.task.name @user.task_assignments.to.name etc. Anyone have any insight into this? -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 9/20/06, Mike <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hey there, > > I''m trying to do a really complex join with ActiveRecord and am coming > up pretty short so far. > > The structure is like: > > user > id > name > etc. > > task > name > ect. > > task_assignments > from_user_id > to_user_id > task_id > > I''m not sure how to model it correctly. I checked out the has_many > :through post about doing self referential joins, but that''s for getting > a user from a user. I want to get the actual task_assignments from a > user: > > @user.task_assignments.task.name > @user.task_assignments.to.name > > etc. > > Anyone have any insight into this?@user.task_assignments is a list of tasks, so you can''t do @user.task_assignments.task.name - how should the interpreter know which task assignment (out of many) you want the task for? You''ll have to iterate through the tasks in some way, for example to collect all the names: names = @user.task_assignments.collect { |assignment| assignment.task.name } or <% for assignment in @user.task_assignments %> <%= assignment.task.name %> <%= assignment.to.name %> <% end %> You can also save yourself some typing by declaring class User has_many :tasks, :through=>task_assignments end Read up on "has_many through", that should give you some ideas. Cheers, Max --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Actually, the issue was, the has_many: through join I was using, linked the user, to... the user, not the task. That was the problem. I''m wonder how to link a user to the actual task itself. Thanks for the comment. Max Muermann wrote:> On 9/20/06, Mike <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> name >> >> Anyone have any insight into this? > @user.task_assignments is a list of tasks, so you can''t do > @user.task_assignments.task.name - how should the interpreter know > which task assignment (out of many) you want the task for? > > You''ll have to iterate through the tasks in some way, for example to > collect all the names: > > names = @user.task_assignments.collect { |assignment| > assignment.task.name } > > or > > <% for assignment in @user.task_assignments %> > <%= assignment.task.name %> > <%= assignment.to.name %> > <% end %> > > You can also save yourself some typing by declaring > > class User > has_many :tasks, :through=>task_assignments > end > > Read up on "has_many through", that should give you some ideas. > > Cheers, > Max-- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Sep 20, 2006, at 11:02 AM, Mike wrote:> Max Muermann wrote: >> On 9/20/06, Mike <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >>> Hey there, >>> >>> I''m trying to do a really complex join with ActiveRecord and am >>> coming >>> up pretty short so far. >>> >>> The structure is like: >>> >>> user >>> id >>> name >>> etc. >>> >>> task >>> name >>> ect. >>> >>> task_assignments >>> from_user_id >>> to_user_id >>> task_id >>> >>> I''m not sure how to model it correctly. I checked out the has_many >>> :through post about doing self referential joins, but that''s for >>> getting >>> a user from a user. I want to get the actual task_assignments from a >>> user: >>> >>> @user.task_assignments.task.name >>> @user.task_assignments.to.name >>> >>> etc. >>> >>> Anyone have any insight into this? >> @user.task_assignments is a list of tasks, so you can''t do >> @user.task_assignments.task.name - how should the interpreter know >> which task assignment (out of many) you want the task for? >> >> You''ll have to iterate through the tasks in some way, for example to >> collect all the names: >> >> names = @user.task_assignments.collect { |assignment| >> assignment.task.name } >> >> or >> >> <% for assignment in @user.task_assignments %> >> <%= assignment.task.name %> >> <%= assignment.to.name %> >> <% end %> >> >> You can also save yourself some typing by declaring >> >> class User >> has_many :tasks, :through=>task_assignments >> end >> >> Read up on "has_many through", that should give you some ideas. >> >> Cheers, >> Max> Actually, the issue was, the has_many: through join I was using, > linked > the user, to... the user, not the task. That was the problem. > > I''m wonder how to link a user to the actual task itself. > > Thanks for the comment.class User < ActiveRecord::Base has_many :task_assignments, :foreign_key => ''to_user_id'' has_many :tasks, :through => :task_assignments has_many :delegated_assignments, :class_name => ''TaskAssignments'', :foreign_key => ''from_user_id'' has_many :delegated_tasks, :through => :delegated_assignments end class Task < ActiveRecord::Base end class TaskAssignments < ActiveRecord::Base belongs_to :task belongs_to :from_user, :class_name => ''User'', :foreign_key => ''from_user_id'' belongs_to :to_user, :class_name => ''User'', :foreign_key => ''to_user_id'' end puts "Tasks for #{@user.name}:" for assignment in @user.tasks puts " #{assignment.task.name} from #{assignment.from_user.name}" end This may not be exactly how you''re setup works, but I think it''s accurate. Please let us know how it goes. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org