Odd Join

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?

On 9/20/06, Mike [email protected] 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

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 M. wrote:

On 9/20/06, Mike [email protected] 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

On Sep 20, 2006, at 11:02 AM, Mike wrote:

user
to_user_id

names = @user.task_assignments.collect { |assignment|

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 B. http://agileconsultingllc.com
[email protected]