Conrad Taylor wrote in post #978329:
> Hi, in tour relationship below, you simply need has_one or belongs_to
> but not both. It really depends where you would like the foreign key.
I think you may have misread the original question. The OP seems to be
asking how to relate one job to two different workers. For this he would
need two separate foreign keys in his Job model:
Job < AR
belongs_to :creator (an instance of a worker)
belongs_to :checker (a different worker)
end
Here's one way that could be accomplished:
The jobs table
+----+------------+------------+-----+
| id | creator_id | checker_id | ... |
| 1 | 3 | 7 | ... |
| 2 | 1 | 4 | ... |
+----+------------+------------+-----+
Job < AR
belongs_to :creator, :class_name => "Worker", :foreign_key =>
"creator_id"
belongs_to :checker, :class_name => "Worker", :foreign_key =>
"checker_id"
end
Worker < AR
has_many :created_jobs, :class_name => "Worker", :foreign_key =>
"creator_id"
has_many :checked_jobs, :class_name => "Worker", :foreign_key =>
"checker_id"
end
Providing association methods:
-------------------------
job_1 = Job.find(1)
job_2 = Job.find(2)
puts job_1.creator.id
=> 3
puts job_1.checker.id
=> 7
puts job_2.creator.id
=> 1
puts job_2.checker.id
=> 4
--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment