I have a table with 2 columns, employee_id and job_id. There is no 'id'
column.
It sounds like this is a join table between your Employee and Job models
for a has_and_belongs_to_many relationship.
but I cant seem to get delete or destroy to work. delete is an undefined
method, and destroy fails because [id] = null
Is there any way to get this to work, or do I just need to add an ID
column?
You probably want to remove entries through either the Employee class or
the Job class (depending on which makes the most sense at the time).
For example:
If:
job = Job.find(... # some condition
emp = Employee.find(... # some condition
It sounds like this is a join table between your Employee and Job models
for a has_and_belongs_to_many relationship.
Not as familiar with the mappings, but that sounds right. Heres what I
had in mind.
employees - table with X employees
jobs - table with Y jobs
exclusions - table of jobs that employees want hidden
For example
User A wants to see all of the jobs except for 1,2 and 3
User B wants to see all except 1, 7 and 12.
With the relationship you proposed, can I add and delete records from
the exclusion table without removing the job or the employee? Using the
above, User B now wants to see job 1, and User A wants to hide job 9.
I might be misinterpreting the code you wrote, but would that code just
delete the mapping (exclusion table) or would it delete the emp/job
record and delete the exclusions along with it?
With the relationship you proposed, can I add and delete records from
the exclusion table without removing the job or the employee? Using the
above, User B now wants to see job 1, and User A wants to hide job 9.
That's right. Treat a_job.employees and an_emp.jobs as arrays to add
and remove from. You can create items as you add them if necessary, but
removing them will not delete either item, just the association.
Obviously you can change the name of the association as an_emp.jobs
doesn't read right for excluded jobs and Rails will assume a join table
of employees_jobs so you'll have to specify that, too. Have a look at
the documentation for has_and_belongs_to_many for full details. Also
google for this as there are many good tutorials on how to handle these
relationships.