Robert Walker wrote in post #978348:
> Here's one way that could be accomplished:
>
> The jobs table
> +----+------------+------------+-----+
> | id | creator_id | checker_id | ... |
> | 1 | 3 | 7 | ... |
> | 2 | 1 | 4 | ... |
> +----+------------+------------+-----+
Also note that this technique does not conform to the First Normal Form
(1NF) of database design. An alternative normalized approach would
involve associating Job and Worker though a many-to-many association and
tagging the worker with a role in the join table.
the job_workers join table
+----+--------+-----------+-------------+
| id | job_id | worker_id | role |
| 1 | 1 | 3 | creator |
| 2 | 1 | 7 | checker |
| 3 | 2 | 1 | creator |
| 4 | 2 | 4 | checker |
+----+--------+-----------+-------------+
Job < AR
has_many :job_workers
has_many :workers, :through => :job_workers
end
JobWorker < AR
belongs_to :job
belongs_to :worker
end
Worker < AR
has_many :job_workers
has_many :jobs, :through => :job_workers
end
job_1 = Job.find(1)
job_2 = Job.find(2)
puts job_1.job_workers.each { |jw| puts "#{jw.worker_id }
:#{jw.worker_role}" }
=> 3 : creator
=> 7 : checker
puts job_2.job_workers.each { |jw| puts "#{jw.worker_id} :
#{jw.worker_role}" }
=> 1 : creator
=> 4 : checker
--
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